Swift 不符合可解码/可编码协议
我正在使用以下结构:Swift 不符合可解码/可编码协议,swift,swift4,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 f
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)
}
}
将weightOld更改为weightNew后,出现两个错误:
-类型“Item”不符合协议“Decodable”
-类型“Item”不符合协议“Codable”
如果我省略了“var weightNew:[Weight]”,它会起作用。不知道发生了什么以及如何解决它。。。谢谢你的帮助 一切都需要可编码。到目前为止,您的
Weight
结构是不可编码的。将Weight
更新为可编码,然后Item
将可编码。struct Weight:Codable{…}
struct Weight {
var day:Int
var weight:Double
var type:Bool
}