When a class marked as Codable is made observable by applying the @Observable macro the following warning is emitted:
• Immutable property will not be decoded because it is declared with an initial value which cannot be overwritten
The immutable property is generated by the @Observable macro on the line declaring property _$observationRegistrar:
• @ObservationIgnored private let _$observationRegistrar = Observation.ObservationRegistrar()
A workaround to suppress the warning is to define a CodingKeys enum that includes all of the visible class properties, leaving out the hidden _$observationRegistrar property. This means that the autogenerated Codable conformance cannot be used when making a class observable using the new Observation framework. It also means that mutable (var) properties must be preceded with an underscore when defining them in the CodingKeys enum to match their redefinition in the macro. This seems verbose and unnecessarily exposes underlying details included in the macro expansion.
Reproduction
import SwiftUI
@Observable
class MyObservable: Codable {
var observedInt: Int
init(int: Int) {
self.observedInt = int
}
// enum CodingKeys: CodingKey {
// case _observedInt
// }
}
Expected behavior
I would expect this to compile without a warning.
Environment
I am compiling using Xcode 15.1 beta 3, but I believe this to be an issue using any version of Xcode 15.
Description
When a class marked as Codable is made observable by applying the @Observable macro the following warning is emitted:
The immutable property is generated by the @Observable macro on the line declaring property _$observationRegistrar:
A workaround to suppress the warning is to define a CodingKeys enum that includes all of the visible class properties, leaving out the hidden _$observationRegistrar property. This means that the autogenerated Codable conformance cannot be used when making a class observable using the new Observation framework. It also means that mutable (var) properties must be preceded with an underscore when defining them in the CodingKeys enum to match their redefinition in the macro. This seems verbose and unnecessarily exposes underlying details included in the macro expansion.
Reproduction
Expected behavior
I would expect this to compile without a warning.
Environment
I am compiling using Xcode 15.1 beta 3, but I believe this to be an issue using any version of Xcode 15.
Additional information
No response