does not conform to protocol Decodable / Codable

arakweker picture arakweker · Mar 3, 2018 · Viewed 10.9k times · Source

I'm using the following struct:

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

}

And for weight:

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

After changing weightOld to weightNew I get two errors: - Type 'Item' does not conform to protocol 'Decodable' - Type 'Item' does not conform to protocol 'Codable'

If I leave out 'var weightNew: [Weight]' it works. Don't know what is happening and how to solve it... Help is appreciated.

Answer

rmaddy picture rmaddy · Mar 3, 2018

Everything needs to be Codable. So far your Weight struct is not Codable. Update Weight to be Codable as well and then Item will be Codable.