How to NSKeyedUnarchiver.unarchiveObject

陌路散爱 提交于 2020-01-24 09:20:11

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/55982244/how-to-nskeyedunarchiver-unarchiveobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!