不符合协议Decodable / Codable

19

我正在使用以下结构体:

struct Item : Codable {

    var category:String
    var birthDate:Date
    var switch:Bool
    var weightNew: [Weight]
    var weightOld: Array<Double>
    var createdAt:Date
    var itemIdentifier:UUID
    var completed:Bool

    func saveItem() {
        DataManager.save(self, with: itemIdentifier.uuidString)
    }

    func deleteItem() { DataManager.delete(itemIdentifier.uuidString)
    }

    mutating func markAsCompleted() {
        self.completed = true
        DataManager.save(self, with: itemIdentifier.uuidString)
    }

}

而对于重量:

struct Weight {
    var day:Int
    var weight:Double
    var type:Bool
}

将weightOld更改为weightNew后,我收到了两个错误: - 类型“Item”不符合协议“Decodable” - 类型“Item”不符合协议“Codable”

如果我省略'var weightNew:[Weight]',它可以工作。不知道发生了什么以及如何解决...需要帮助。


5
结构体 Weight:可编码的 { ... }。 - Rob
1个回答

29

一切都需要遵循 Codable 协议。目前你的 Weight 结构体还未遵循 Codable 协议。请更新 Weight 以使其遵循 Codable 协议,然后 Item 就会成为 Codable 的了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接