Hi,
Overview:
- I get the following error when trying to save / read from SwiftData
- It happens when I try to save color to SwiftData (code below)
Error
'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release
Questions
- How can I resolve the error?
- I am not directly using data, I am using just Float values, swift types. Why am I getting this error?
- Is there a way to add a breakpoint to stop at the exact type causing the error? (Symbolic breakpoint doesn't seem to help)
- Or is the below code ok and not responsible for the error?
Code
import SwiftUI
nonisolated struct ColorRepresentation: Codable {
let red: Float
let green: Float
let blue: Float
let opacity: Float
init(colorResolved: Color.Resolved) {
red = colorResolved.red
green = colorResolved.green
blue = colorResolved.blue
opacity = colorResolved.opacity
}
func color() throws -> Color {
Color(
red: Double(red),
green: Double(green),
blue: Double(blue),
opacity: Double(opacity)
)
}
}
extension ColorRepresentation: Equatable {}
I've been quite swamped so hopefully this is not too late...
First, thanks for providing the test project in your feedback report, which does reproduce the issue for me.
Using Codable that way is nothing wrong, and so I'd say that's a framework bug, and thanks for having filed the feedback report (FB22634797) for us.
With your project, I've figured out that my code (as mentioned here) doesn't reproduce the warning because I use an array, and not the Codable type directly:
var timesData2: [SchedulingTime] = []
You can confirm the behavior by changing colorCode in your project to an optional array:
var colorCode: [ColorCode]?
Note that to do a clean test, you need to remove both your local SwiftData store and the records on the CloudKit server with CloudKit Console.
To work around the warning, you can probably consider using a transformable attribute instead. Be sure that your transformer conforms NSSecureCoding, as discussed here.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.