不符合协议可解码/可编码

ara*_*ker 15 swift swift4

我正在使用以下结构:

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)
    }

}
Run Code Online (Sandbox Code Playgroud)

而对于重量:

struct Weight {
    var day:Int
    var weight:Double
    var type:Bool
}
Run Code Online (Sandbox Code Playgroud)

将weightOld改为weightNew后,我得到两个错误: - Type'Item'不符合协议'Decodable' - Type'Item'不符合协议'Codable'

如果我遗漏'var weightNew:[Weight]'就可以了.不知道发生了什么以及如何解决它...帮助表示赞赏.

rma*_*ddy 23

一切都需要是可编码的.到目前为止,您的Weight结构不是Codable.更新Weight为Codable,然后Item将是Codable.