How to NSKeyedUnarchiver.unarchiveObject
The method above didn't worked for me for unarchiving, I did this and it worked:
NSKeyedUnarchiver.unarchivedObject(ofClasses: [Record.self], from: archived)
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
.