I'm getting SwiftData/SchemaProperty.swift:369: Fatal error: Unexpected type for CompositeAttribute CLLocationCoordinate2D in the following situation - this is simplified, but demonstrates the issue.
There are a lot of posts discussing Measurements running into the same issue, and the recommended fix is to turn the Measurement (or CLLocationCoordinate2D in this example) into a computed property and generate it on the fly.
I'm trying to cache instances of CLLocationCoordinate2D, rather than creating new ones every time a View renders, so those work arounds don't work around.
The SwiftData docs seem to indicate that this should be possible. Can anyone recommend a fix?
Thanks in advance.
import SwiftData
import MapKit
@Model
final class foo : Codable {
var bar: SomeStruct
init( bar: SomeStruct ) {
self.bar = bar
}
enum CodingKeys : CodingKey {
case bar
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.bar = try container.decode(SomeStruct.self, forKey: .bar)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(bar, forKey: .bar)
}
}
struct SomeStruct : Codable {
var latitude: Double
var longitude: Double
@Transient
var clLocation: CLLocationCoordinate2D
init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
self.clLocation = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
enum CodingKeys : CodingKey {
case latitude, longitude
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.latitude = try container.decode(Double.self, forKey: .latitude)
self.longitude = try container.decode(Double.self, forKey: .longitude)
self.clLocation = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(latitude, forKey: .latitude)
try container.encode(longitude, forKey: .longitude)
}
}