代码:
func restoreData(){ dispatchBackground_async({ calcTimeStart("RestoreConversationItemList") if let savedFilePath = findSavedFilePath(ArchiveConversationDir, savedFilename: "conversationItemList") { if let conversationItemListObj = NSKeyedUnarchiver.unarchiveObjectWithFile(savedFilePath){ let restoreConversationItemList = conversationItemListObj as! [ConversationItem] self.isRestored = true self.conversationItemList = restoreConversationItemList self.tableView.reloadData() gLog.info("restored \(self.conversationItemList.count) conversations from \(savedFilePath)") } else { gLog.warning("restore conversationItemList fail") } } else { gLog.info("not found saved conversation item list") } calcTimeEnd("RestoreConversationItemList") }) } |
中的:
if let conversationItemListObj = NSKeyedUnarchiver.unarchiveObjectWithFile(savedFilePath){ |
出错:
2016-04-02 11:32:49.935 JianDao[34302:1911704] *** Terminating app due to uncaught exception ‘NSInvalidUnarchiveOperationException’, reason: ‘*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (简道.ConversationItem) for key (NS.objects); the class may be defined in source code or a library that is not linked’ *** First throw call stack: ( 0 CoreFoundation 0x02d40494 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x0444ee02 objc_exception_throw + 50 2 Foundation 0x020425d6 -[NSCoder(Except |
NSKeyedUnarchiver decodeObjectForKey cannot decode object of class for key NS.objects the class may be defined in source code or a library that is not linked
Decode object in WatchKit Extension | Apple Developer Forums
ios – NSKeyedUnarchiver error after renaming Xcode project – Stack Overflow
ios – How can I decode an object when original class is not available? – Stack Overflow
基本上确定是之前的:
把app的名字,从中文 简道 改为英文 JianDao 而导致找不到之前的
简道.ConversationItem
了,
现在去,删除之前的旧程序,重现安装,应该是可以解决的
但是,还是为了保险起见
想要去给这个函数,加上异常处理,至少当decode出错时,不至于导致程序的崩溃
结果想要加上try catch时却又提示
No calls to throwing functions occur within ‘try’ expression
所以算了,还是不去加了。
还是删除旧程序,重新安装新程序吧。
然后重新安装程序,即可解决问题。
转载请注明:在路上 » [已解决]NSInvalidUnarchiveOperationException NSKeyedUnarchiver decodeObjectForKey cannot decode object of class for key NS.objects