Hello - I noticed that if I mark a Codable
class as @Observable
, the compiler gives a warning inside the generated code.
@Observable
final class User: Codable {
let id: Int
let name: String
}
which expands to...
@Observable
final class User: Codable {
let id: Int
let name: String
/* generated */
@ObservationIgnored private let _$observationRegistrar = Observation.ObservationRegistrar() // ⚠️ Immutable property will not be decoded because it is declared with an initial value which cannot be overwritten
internal nonisolated func access<Member>(
keyPath: KeyPath<User, Member>
) {
_$observationRegistrar.access(self, keyPath: keyPath)
}
internal nonisolated func withMutation<Member, MutationResult>(
keyPath: KeyPath<User, Member>,
_ mutation: () throws -> MutationResult
) rethrows -> MutationResult {
try _$observationRegistrar.withMutation(of: self, keyPath: keyPath, mutation)
}
/* end generated */
}
Is there any workaround here or are objects marked as @Observable
not intended to work with the generated Codable
conformance? A custom implementation of encode
/ init(from decoder:)
fixes the warning.