iOS 深度好文

[NSKeyedUnarchiver decodeObjectF

2019-12-10  本文已影响0人  落夏简叶

问题:

[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (Installation) for key (root); the class may be defined in source code or a library that is not linked

一般出现在用archive序列化存储一个对象A,然后后来的某个版本中在对象A中添加了属性。然后解档老的序列化数据时crash

解决方案:


NSKeyedArchiver.setClassName("Employee", for: Employee.self)
//before serializing
let employeeData = NSKeyedArchiver.archivedData(withRootObject: employee)


NSKeyedUnarchiver.setClass(Employee.self, forClassName: "Employee")
//before deserializing
NSKeyedUnarchiver.unarchiveObject(with: decoded) as? Employee
上一篇下一篇

猜你喜欢

热点阅读