If the coder responds true to requiresSecureCoding, then the coder calls failWithError(_:) in either of the following cases: The class indicated by cls doesn’t implement NSSecureCoding. The unarchived class doesn’t match cls, nor do any of its superclasses.
If the coder doesn’t require secure coding, it ignores the cls parameter and does not check the decoded object.
The class must subclass from NSObject and conform to NSSecureCoding
requiredinit?(coder aDecoder: NSCoder) { guard let id = aDecoder.decodeObject(of: [NSString.self], forKey: "id") as? String, let text = aDecoder.decodeObject(of: [NSString.self], forKey: "text") as? String, let date = aDecoder.decodeObject(of: [NSDate.self], forKey: "date") as? Date else { returnnil }
self.id = id self.text = text self.date = date }
let id: String var text = "untitled" var date: Date = Date()
overrideinit() { id = UUID().uuidString super.init() } }
First, we need to serialize to Data, then use EasyStash for easy persistency
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
do { let securedItems = items.map({ SecuredClientLoggerItem(item: $0) }) if #available(iOS 11.0, *) { let data = tryNSKeyedArchiver.archivedData( withRootObject: securedItems, requiringSecureCoding: true )
Then we can use unarchiveTopLevelObjectWithData to unarchive array
1 2 3 4 5 6 7
do { let data = tryData(contentsOf: fileUrl) let notes = tryNSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? [Note] // notes is of type [Note]? } catch { print(error) }
Note that for UUID, NSCoding seems to convert to UUID instead of String
1 2 3 4
let id = aDecoder.decodeObject( of: [NSUUID.self], forKey: "id" ) as? UUID,