swiftios12nskeyedunarchiver

How to NSKeyedUnarchiver.unarchiveObject


I have a working code that works but deprecated:

This part is fine:

 let archived = try? NSKeyedArchiver.archivedData(withRootObject: [defaultRecord] as NSArray, requiringSecureCoding: false)

This is Deprecated:

 let records = NSKeyedUnarchiver.unarchiveObject(with: unarchivedObject as Data) as? [Record]

'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead

Sounds simple as it is, I couldn't find a way to use the suggested method without getting errors in every combination I tried.

Any working example of this?


Solution

  • The usage of the new API to archive an array is a bit tricky.

    You could have figured it out yourself if you wouldn't ignore the errors with try? 😉

    To be able to decode an array of a custom class with unarchivedObject(ofClass:from: you have to use the plural form unarchivedObject(ofClasses:from: and specify both NSArray(!) and the custom class. Further your class must adopt NSSecureCoding

    class Record : NSObject, NSSecureCoding {
    
       static var supportsSecureCoding: Bool {
            return true
        }
    
    ....
    
    do {
        let archived = try NSKeyedArchiver.archivedData(withRootObject: [defaultRecord], requiringSecureCoding: false)
    
        let records = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self, Record.self], from: archived) as! [Record]
        print(records)
    } catch { print(error) }
    

    But why do you archive defaultRecord as array at all? If you archive the single object you can leave your class as it is and write

    do {
        let archived = try NSKeyedArchiver.archivedData(withRootObject: defaultRecord, requiringSecureCoding: false)
    
        let record = try NSKeyedUnarchiver.unarchivedObject(ofClass: Record.self, from: archived)
        let records = [record]
        print(records)
    } catch { print(error) }
    

    Side note: Consider to serialize the class with Codable. It's swiftier and doesn't require inheritance from NSObject.