I'm just starting out on learning SwiftUI. Where do I go wrong? I'm trying to add Codable conformance in my class (ManyItems). This so I eventually can save an array to disk using JSON. Two errors:
1) In both the "required init(...) "id = try..." and the encode func: "try container.encode..." result in "'id' is unavailable in Swift: 'id' is not available in Swift; use 'Any'"
2) In both the required init(...) and func encode: "Use of unresolved identifier 'one'." I assumed the identifier in the struct would be carried forward into the class?
struct Item: Identifiable {
var id = UUID()
var one: String
}
class ManyItems: ObservableObject, Codable {
@Published var manyitems = [Item]()
enum CodingKeys: CodingKey {
case id
case one
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UUID.self, forKey: .id)
one = try container.decode(String.self, forKey: .one)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(one, forKey: .one)
}
}