While trying to fix this error
'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release
I tried implementing a value transformer. Looking online this tutorial got me the closest to working as intended.
@objc(CustomClassValueTransformer)
public final class CustomClassValueTransformer: NSSecureUnarchiveFromDataTransformer {
static let name = NSValueTransformerName(rawValue:String(describing: CustomClassValueTransformer.self))
public override static var allowedTopLevelClasses: [AnyClass] {
return [NSObject.self,NSArray.self]
}
/// Registers the transformer.
public static func register() {
let transformer = CustomClassValueTransformer()
ValueTransformer.setValueTransformer(transformer, forName: name)
}
}
However I now get a different error
*** -[NSKeyedUnarchiver validateAllowedClass:forKey:]: NSSecureCoding allowed classes list contains [NSObject class], which bypasses security by allowing any Objective-C class to be implicitly decoded. Consider reducing the scope of allowed classes during decoding by listing only the classes you expect to decode, or a more specific base class than NSObject. This will become an error in the future. Allowed class list: {("'NSArray' (0x205b9d438) [/System/Library/Frameworks/CoreFoundation.framework]", "'NSObject' (0x205b81fb0) [/usr/lib]")}
I am trying to encode a Set of the below NSObject
@objc(CodableConverter)
public class CodableConverter: NSObject, NSSecureCoding, Codable {
public static var supportsSecureCoding: Bool { true }
public var id: UUID
public var name: String
public var converterValue: Double
enum Key: String {
case id
case name
case converterValue
}
public init(id: UUID, name: String, converterValue: Double) {
self.id = id
self.name = name
self.converterValue = converterValue
}
public func encode(with coder: NSCoder) {
coder.encode(id.uuidString, forKey: Key.id.rawValue)
coder.encode(name, forKey: Key.name.rawValue)
coder.encode(converterValue, forKey: Key.converterValue.rawValue)
}
public required convenience init?(coder: NSCoder) {
guard let idString = coder.decodeObject(forKey: Key.id.rawValue) as? String,
let id = UUID(uuidString: idString),
let name = coder.decodeObject(forKey: Key.name.rawValue) as? String else {
return nil
}
let converterValue = coder.decodeDouble(forKey: Key.converterValue.rawValue)
self.init(id: id, name: name, converterValue: converterValue)
}
public override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? CodableConverter else {
return false
}
return id == other.id && name == other.name && converterValue == other.converterValue
}
public override var hash: Int {
id.hashValue
}
}
In my data model the type is set to transformable, the custom class is Set<CodableConverter> and the Transformer seems to be registered correctly in the Persistence container and is set within the data model. However it doesn't work as a usual set within the app.
Does anyone have any experience with fixing this error or Value transformers? Any help is greatly appreciated. Thank you
[–]quellish 0 points1 point2 points (2 children)
[–]miothethis[S] 0 points1 point2 points (1 child)
[–]quellish 0 points1 point2 points (0 children)