@Observable class not compatible with Codable?

So any time I create a class that's both @Observable and Codable, e.g.

@Observable class GameLocationManager : Codable {

I get a warning in the macro expansion code:

@ObservationIgnored private let _$observationRegistrar = Observation.ObservationRegistrar()

Immutable property will not be decoded because it is declared with an initial value which cannot be overwritten.

I've been ignoring them for now, but there are at least a half a dozen of them now in my (relatively small) codebase, and I'd like to find a solution (ideally one that doesn't require me to write init(decoder:) for every @Observable class in my project...), especially since I'm not sure what the actual consequences of ignoring this might be.

Answered by DTS Engineer in 797303022

This seems to silence the warning:

@Observable class GameLocationManager: Codable {

    init(hasPointyHair: Bool) {
        self.hasPointyHair = hasPointyHair
    }

    var hasPointyHair: Bool
    
    enum CodingKeys: String, CodingKey {
        case _hasPointyHair = "hasPointyHair"
    }
}

There are three parts to this:

  • By supplying a custom CodingKeys value, you tell the coders to ignore the _$observationRegistrar property.

  • The enum case must be _hasPointyHair because that’s the name of the property storage backing the public hasPointyHair property.

  • And the value must be hasPointyHair so that you get the right JSON.

since I'm not sure what the actual consequences of ignoring this might be.

I don’t think this is a significant warning, to the point where I’m not sure whether it’s worth going through the rigamarole I’ve shown above. I do think you should file a bug against Xcode though. It seems like these two technologies should be compatible out of the box.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

This seems to silence the warning:

@Observable class GameLocationManager: Codable {

    init(hasPointyHair: Bool) {
        self.hasPointyHair = hasPointyHair
    }

    var hasPointyHair: Bool
    
    enum CodingKeys: String, CodingKey {
        case _hasPointyHair = "hasPointyHair"
    }
}

There are three parts to this:

  • By supplying a custom CodingKeys value, you tell the coders to ignore the _$observationRegistrar property.

  • The enum case must be _hasPointyHair because that’s the name of the property storage backing the public hasPointyHair property.

  • And the value must be hasPointyHair so that you get the right JSON.

since I'm not sure what the actual consequences of ignoring this might be.

I don’t think this is a significant warning, to the point where I’m not sure whether it’s worth going through the rigamarole I’ve shown above. I do think you should file a bug against Xcode though. It seems like these two technologies should be compatible out of the box.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@Observable class not compatible with Codable?
 
 
Q