SwiftData Transformable with String Array

I am doing a full transition of an app from CoreData to SwiftData. I was able to follow the online guides and extract SwiftData objects from the CoreData model. Everything seems to work except for my Transformable String Arrays. This app is storing CoreData in iCloud.

In my SwiftData model, I have a variable...

@Attribute(.transformable(by: "NSSecureUnarchiveFromData"))
var arrayofStrings: [String]?

The app is able to read the array of strings, however, the debugger gives me this error eventually crashing due to memory.

'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release

I don't understand, the transformed variable already is 'NSSecureUnarchiveFromData' not 'NSKeyedUnarchiveFromData'. That's the reason why I used NSSecureUnarchiveFromData in my CoreData model because NSKeyedUnarchiveFromData is being phased out. I don't get why the debugger thinks otherwise.

Any thoughts?

Answered by phspman in 783133022

Ok, I think I figured it out. So.. when you generate SwiftData Objects from Core Data and if you have Transformable Data like above what I've done with an Array of Strings, you will get

@Attribute(.transformable(by: "NSSecureUnarchiveFromData"))
var yourArrayOfStrings: [String]?

However, SwiftData is so good that it already recognizes these Array of Strings so having that Attribute confuses Xcode to thinking you're using NSKeyedUnarchiveFromData. The solution is to just delete that attribute like so...

var yourArrayOfStrings: [String]?

And it just works! The warning goes away in the Debugger.

Here is what the Transformer looks like in the CoreData version of the app. I tried changing to @Attribute(.transformable(by: "NSSecureUnarchiveFromDataTransformer")) too.

It seems with a new Xcode update, the app doesn't crash anymore but still produces the error every time the variable is read from the model. So in a view when it's repeatedly read. The error floods my debugger and doesn't stop.

Accepted Answer

Ok, I think I figured it out. So.. when you generate SwiftData Objects from Core Data and if you have Transformable Data like above what I've done with an Array of Strings, you will get

@Attribute(.transformable(by: "NSSecureUnarchiveFromData"))
var yourArrayOfStrings: [String]?

However, SwiftData is so good that it already recognizes these Array of Strings so having that Attribute confuses Xcode to thinking you're using NSKeyedUnarchiveFromData. The solution is to just delete that attribute like so...

var yourArrayOfStrings: [String]?

And it just works! The warning goes away in the Debugger.

SwiftData Transformable with String Array
 
 
Q