OK. The original version (before challenge 3) addressed the published/Codable issue by adding encode/decode steps for the class.
This challenge was to wrap a struct in a class which I thought was intended to simplify the codable problem. So, I am looking for a way to encode the class which does not have the vars used in the CodingKey or in the required init.
Not sure how this is going to be simpler right now?!
Here is where I am with an error on the required init for the struct, and no simplification...
class Order: ObservableObject {
@Published var orderDetail = OrderDetail()
}
struct OrderDetail: Codable {
static let types = ["Vanilla", "Strawberry", "Chocolate", "Rainbow"]
//Make vars Codable
enum CodingKeys: CodingKey {
case type, quantity, extraFrosting, addSprinkles, name, streetAddress, city, zip
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)
try container.encode(quantity, forKey: .quantity)
try container.encode(extraFrosting, forKey: .extraFrosting)
try container.encode(addSprinkles, forKey: .addSprinkles)
try container.encode(name, forKey: .name)
try container.encode(streetAddress, forKey: .streetAddress)
try container.encode(city, forKey: .city)
try container.encode(zip, forKey: .zip)
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
type = try container.decode(Int.self, forKey: .type)
quantity = try container.decode(Int.self, forKey: .quantity)
extraFrosting = try container.decode(Bool.self, forKey: .extraFrosting)
addSprinkles = try container.decode(Bool.self, forKey: .addSprinkles)
name = try container.decode(String.self, forKey: .name)
streetAddress = try container.decode(String.self, forKey: .streetAddress)
city = try container.decode(String.self, forKey: .city)
zip = try container.decode(String.self, forKey: .zip)
}
//Initializer for new empty orders using defaults below
init() { }
var type = 0
var quantity = 3
var name = ""
var streetAddress = ""
var city = ""
var zip = ""
....