Search code examples
iosswiftcore-datatransformable

Transformable and NSKeyedUnarchiveFromData


I'm trying to save a Measurement to a Core Data transformable property. Basically, what's in this answer, only a different unit: How to Save a Generic Measurement<Unit> in Core Data?

Everything works, however I'm getting this message in the log: [general] 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release

I'm not sure how to fix this. Or what exactly it means. This is my first time working with a transformable Core Data property.

Thanks


Edit:

I made a new class:

import Foundation

@objc(UnitTemperatureValueTransformer)
final class UnitTemperatureValueTransformer: NSSecureUnarchiveFromDataTransformer {
    static let name = NSValueTransformerName(rawValue: String(describing: UnitTemperatureValueTransformer.self))
    override static var allowedTopLevelClasses: [AnyClass] {
        return [UnitTemperature.self]
    }
    
    public static func register() {
        let transformer = UnitTemperatureValueTransformer()
        ValueTransformer.setValueTransformer(transformer, forName: name)
    }
}

And then I set the transformer value for the Core Data object to: UnitTemperatureValueTransformer.

Is that all that needs to be done?


Solution

  • This warning appears because apple will remove it in future release because it's not secure by default. Here is thread on Apple forum. You should use NSSecureUnarchiveFromData instead of that.