I've run into an issue where setting a property on a SwiftData object to a Codable
struct with an optional String crashes the app.
For example, if I have the following model object and struct :
@Model
final class Item {
var timestamp: Date
var codableProp: CodedValue?
init(timestamp: Date, cProp: CodedValue?) {
self.timestamp = timestamp
self.codableProp = cProp
}
}
struct CodedValue: Codable{
var string0: String? = nil
var bool0: Bool? = nil
var bool1: Bool? = nil
}
If I create an instance of CodedValue
with the following JSON, the app crashes:
{
"bool0": true
}
The specific error I'm getting is:
"Could not cast value of type 'Swift.Optional<Swift.String>' (0x1c1e3f000) to 'Swift.String' (0x1c1e37e30)"