Core Data & Xcode 11: Please switch to using "NSSecureUnarchiveFromData" or a subclass of NSSecureUnarchiveFromDataTransformer
Setting Transformer property to NSSecureUnarchiveFromDataTransformer solved the warning in my case. For this select the attribute & set its transformer type to NSSecureUnarchiveFromDataTransformer & run again by pressing commond+R.
Thanks,Ratneshwar
This is related to a migration from NSCoding
to the NSSecureCoding
protocol. The default ValueTransformer
adopts NSCoding
, so the only solution that worked for me was to write my own Transformer
that adopts the NSSecureUnarchiveFromDataTransformer
protocol.
I should say that my own experience is from trying to define an Attribute with the Transformer type to persist a custom class that adopted NSCoding
. I was initially met with a warning similar to the OP's error. I was able to suppress the warning by changing the Transformer field on the attribute to "NSSecureUnarchiveFromData" as others have mentioned, but I then received an error along the lines of:Not able to save to CoreData. SQLCore dispatchRequest Object of class “ ” not among allowed top level class list...
as mentioned here. The suggestion to change the Attribute to a Relationship was undesirable in my case.
More digging came up with this blog post that details the "reason" for all of this, and gives a solution that worked for me. The blog actually uses the case of UIColor
in the example, but it works for any custom class as well.
Say you have a CustomClass
that you want to store as a Transformable Attribute in some Entity. If you're like me, you may have adopted NSCoding
and received the aforementioned error. The solution would be to adopt NSSecureCoding
instead and define an NSSecureUnarchiveFromDataTransformer
subclass:
@objc(CustomClassValueTransformer)final class CustomClassValueTransformer: NSSecureUnarchiveFromDataTransformer { static let name = NSValueTransformerName(rawValue: String(describing: CustomClass.self)) // Make sure `CustomClass` is in the allowed class list, // AND any other classes that are encoded in `CustomClass` override static var allowedTopLevelClasses: [AnyClass] { // for example... yours may look different return [CustomClass.self, OtherClass.self, NSArray.self, NSValue.self] } /// Registers the transformer. public static func register() { let transformer = CustomClassValueTransformer() ValueTransformer.setValueTransformer(transformer, forName: name) }}
Then make sure to set the Transformer field on your Attribute to "CustomClassValueTransformer" and the Custom Class field to "CustomClass" and you should be good to go.
Swift 5.4.2
This worked for me.
EDIT Link to the article is here.
- Click on the .xcdatamodeld file in the project navigator
- Click on theEntity that has a Transformable Attribute
- Click on the Transformable Attribute
- Click the 'Show Data Model Inspector' icon
- Enter 'NSSecureUnarchiveFromDataTransformer'in the Transformer field
Your warnings/errors should go away. If not, try cleaning your build folder and rebuild.