Post

Replies

Boosts

Views

Activity

Reply to SwiftData: Failed to decode a composite attribute
A migration is probably the best solution but if you don't want to do that you could use a custom decoding for the enum extension Kind { init(from decoder: any Decoder) throws { let container = try decoder.singleValueContainer() let rawValue = try container.decode(String.self) if let kind = Kind(rawValue: rawValue) { self = kind } else { let oldValue = rawValue.capitalized // <-- You might need to adjust this depending on how the rest of the enum looks. guard let kind = Kind(rawValue: oldValue) else { throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Unknow kind: \(oldValue)")) } self = kind } } } Note that this will fix the value when read from storage but to actually store the new value, "Credit", each objects will need to be saved until it's persisted.
4d
Reply to SwiftData error "Thread 1: Fatal error: Composite Coder only supports Keyed Container"
If you create a Color.Resolved value in a playground and encodes it you see you get an array of doubles rather than a structure with RGB properties and the error clearly states that an array (keyless container) is not supported. So I guess the conclusion is that Color.Resolved isn't supported by SwiftData. My playground test code and output let color = Color.red var resolvedColor: Color.Resolved resolvedColor = color.resolve(in: EnvironmentValues()) let data = try! JSONEncoder().encode(resolvedColor) print(String(data: data, encoding: .utf8)!) [1,0.23137254,0.18823528,1]
1w
Reply to [SwiftData] How to get the first 7 elements by using @Query?
If you create the query property using a FetchDescriptor then you can set a limit for the number of rows being fetched. The drawback of this solution is that it's not a one liner so you need to do it in the init @Query private var records:[Record] init() { var fetchDescriptor = FetchDescriptor<Record>(sortBy: [SortDescriptor(\Record.date, order: .reverse)]) fetchDescriptor.fetchLimit = 7 _categories = Query(fetchDescriptor) } If you for some reason don't want to do it in the init you could declare the fetch descriptor as a static variable and then pass it to the @Query declaration
1w