Coredata error with supportsSecureCoding

Hello, please tell me where am I wrong?

Failure to save context: Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSUnderlyingError=0x600003555dd0 {Error Domain=NSCocoaErrorDomain Code=4866 "Данные не удалось записать, так как они имеют неверный формат." UserInfo={NSUnderlyingError=0x600003557090 {Error Domain=NSCocoaErrorDomain Code=4864 "Class 'InStat.Preferences' disallows secure coding. It must return YES from supportsSecureCoding." UserInfo={NSDebugDescription=Class 'InStat.Preferences' disallows secure coding. It must return YES from supportsSecureCoding.}}}}}

i tried public static var supportsSecureCoding = true
Used both NSSecureUnarchiveFromDataTransformer and NSSecureUnarchiveFromData


public class Preferences: NSObject, NSCoding {
    var id = 0
    var name = LanguageItem<String>(values: [:], defaultLanguage: "")
    var subName = LanguageItem<String>(values: [:], defaultLanguage: "")
    var flag = URL(string: "")
    var iconSport = ""
    var kindOfSport = 0
    var isSelected = false
init(id: Int, name: LanguageItem<String>, subName: LanguageItem<String>, flag: URL, iconSport: String, kindOfSport: Int, isSelected: Bool) {
    self.id = id
    self.name = name
    self.subName = subName
    self.iconSport = iconSport
    self.flag = flag
    self.kindOfSport = kindOfSport
    self.isSelected = isSelected
}

enum CodingKeys: String, CodingKey {
    case id, name, subName, iconSport, flag, kindOfSport, isSelected
}

public override init() {
    super.init()
}
public func encode(with aCoder: NSCoder) {
    aCoder.encode(id, forKey: CodingKeys.id.rawValue)
    aCoder.encode(name, forKey: CodingKeys.name.rawValue)
    aCoder.encode(subName, forKey: CodingKeys.subName.rawValue)
    aCoder.encode(iconSport, forKey: CodingKeys.iconSport.rawValue)
    aCoder.encode(flag, forKey: CodingKeys.flag.rawValue)
    aCoder.encode(kindOfSport, forKey: CodingKeys.kindOfSport.rawValue)
    aCoder.encode(isSelected, forKey: CodingKeys.isSelected.rawValue)
}


public required convenience init?(coder aDecoder: NSCoder) {
    let id = aDecoder.decodeObject(forKey: CodingKeys.id.rawValue) as? Int ?? 0
    let name = aDecoder.decodeObject(forKey: CodingKeys.name.rawValue) as? LanguageItem<String> ?? LanguageItem<String>(values: [:], defaultLanguage: "")
    let subName = aDecoder.decodeObject(forKey: CodingKeys.subName.rawValue) as? LanguageItem<String> ?? LanguageItem<String>(values: [:], defaultLanguage: "")
    let iconSport = aDecoder.decodeObject(forKey: CodingKeys.iconSport.rawValue)  as? String ?? ""
    let flag = aDecoder.decodeObject(forKey: CodingKeys.flag.rawValue) as? URL ?? URL(string: "")
    let kindOfSport = aDecoder.decodeObject(forKey: CodingKeys.kindOfSport.rawValue) as? Int ?? 0
    let isSelected = aDecoder.decodeObject(forKey: CodingKeys.isSelected.rawValue) as? Bool ?? false
    self.init(id: id, name: name, subName: subName, flag: (flag ?? URL(string: ""))!, iconSport: iconSport, kindOfSport: kindOfSport, isSelected: isSelected)
}

Your type must conform to NSSecureCoding, which is what contains the static var supportsSecureCoding: Bool { get } requirement.

See NSSecureCoding and transformable properties in Core Data

yes i used this together with NSSecureCoding, the error remained
Screenshot 2021-06-26 at 21.56.43

Terms of Service

Privacy Policy

Cookie Policy