This possibly seems like a regression but from iOS 17.1.0+, I'm having issues from Xcode 15.2 Beta &where when using a transformable property I'm getting a crash when trying to create a model container. This worked fine for me in Xcode 15.1 Beta when testing on iOS OS 17.0.1 and below.
I have a simple model where I'm trying to save a UIColor, below is an example of this model.
class Category: Codable {
@Attribute(.unique)
var title: String
var items: [Item]?
@Attribute(.transformable(by: ColorValueTransformer.self))
var color: UIColor?
init(title: String = "",
color: UIColor) {
self.title = title
self.color = color
}
enum CodingKeys: String, CodingKey {
case title
}
required init(from decoder: Decoder) throws { ... }
func encode(to encoder: Encoder) throws { ... }
}
Within my value transformer, I'm handling setting and getting the value.
final class ColorValueTransformer: ValueTransformer {
static let name = NSValueTransformerName(rawValue: String(describing: ColorValueTransformer.self))
override func transformedValue(_ value: Any?) -> Any? {
guard let color = value as? UIColor else { return nil }
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: color, requiringSecureCoding: true)
return data
} catch {
return nil
}
}
override func reverseTransformedValue(_ value: Any?) -> Any? {
guard let data = value as? Data else { return nil }
do {
let color = try NSKeyedUnarchiver.unarchivedObject(ofClass: UIColor.self, from: data)
return color
} catch {
return nil
}
}
public static func register() {
let transformer = ColorValueTransformer()
ValueTransformer.setValueTransformer(transformer, forName: name)
}
}
Then within my root app entry point, I register this transformer.
@main
struct ToDosApp: App {
......
init() {
ColorValueTransformer.register()
}
......
Unfortunately in my custom container object I get a crash on this line
let container = try ModelContainer(for: ....)
With an error of Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
Like i said before previously this was working fine but now it's not... I have a feedback open also FB13471979 but it would be great if someone on the SwiftData team at Apple could look into this issue since it's a pretty big regression...