Help ,Codable class, @Published

Why do I get plenty of errors when trying to conform class to Codable?
Code Block
class Order: ObservableObject, Codable {
    
    enum CodingKeys: CodingKey {
           case  name
       }
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try container.decode(String.self, forKey: .name)
    }
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(name, forKey: .name)
    }
    
    var didChange = PassthroughSubject<Void,Never>()
    
    static let types = ["Vanilla","Chocolate", "Strawberry", "Rainbow"]
    
    @Published var type = 0 { didSet { update() } }
    
    @Published var quantity = 3 { didSet { update() } }
    @Published var specialRequests = false { didSet { update() } }
    @Published var extraSprinkles = false { didSet { update() } }
    @Published var extraFrosting = false { didSet { update() } }
    
    @Published var name = ""  { didSet { update() } }
    @Published var city = ""{ didSet { update() } }
    @Published var streetAddress = ""{ didSet { update() } }
    @Published var zip = ""{ didSet { update() } }
    
    @Published var isValid = true{ didSet { update() } }
    
    func update(){
        didChange.send(())
    }
    


Plenty of errors is not a very precise information.
Can you post the errors and where they occur ?
Your code compiles without errors with Xcode 11.6 or 12 beta 4 if I filled
  • import Combine at the top

  • Closing brace } at the bottom

Does your source file have both of them?


By the way, do you really need didChange? ObservableObject has a property named objectWillChange and in many typical cases, that is enough to keep track of @Published vars, objectWillChange.send() will be called automatically for @Published vars.
Your code looks like an old code example using BindableObject seen in Xcode 11 betas.

ObservableObject acts as "the store" or "state" of the view. It should be on separate class then the view tracks user adding or removing items of ObservableObject with @ObservedObject
Help ,Codable class, @Published
 
 
Q