支持CoreData的App运行时报错了:
, *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (DateRange) for key (root); the class may be defined in source code or a library that is not linked with userInfo of { "__NSCoderInternalErrorCode" = 4864; }
下面还有更进一步的错误:
Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (DateRange) for key (root); the class may be defined in source code or a library that is not linked'
从中大致可以看到原因是root key对应的类DateRange无法被解码导致的!
我们知道CoreData在保存数据时会将其类型名称也一并保存,如果你在后来改变了类名称,自然会崩。
但是回想一下,我并没有改变任何Swift类的名称啊!!!
wait!但我的确修改了对应Swift类导出到ObjC类的名称:
@objc(HyDateRange) //原来是DateRange! public class HyDateRange:NSObject,NSCopying,NSCoding{}
所以我们得出了:对于CoreData中的托管类,最终保存的是其ObjC类中的名称,这可能是因为CoreData的实际底层功能还是用ObjC来完成的。
解决办法是将App删除,让其自行重新创建数据库就可以了。