NSKeyedUnarchiver error after renaming Xcode proje

2019-01-26 12:38发布

I just renamed my Xcode project and when I ran it I got this error:

2015-11-14 05:32:42.337 Buck Tracker[3537:1456100] * Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '* -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (iBudgeter.Record) for key (NS.objects); the class may be defined in source code or a library that is not linked'

The Buck Tracker is the new name and iBudgeter is the original name. Record is a custom NSObject I created to store some data.

I tried renaming the project back to iBudgeter but it didn't work. Reverting to a previous version in git did help but I got the same error when I renamed it again.

So any suggestions?

4条回答
劳资没心,怎么记你
2楼-- · 2019-01-26 13:08

Don't change your project name. Just change the display name. It's the "Bundle display name" entry in your Info.plist. You'll probably need to add the entry.

adding Bundle display name to Info.plist

See this answer if you want to change the display name of an OS X app.

查看更多
迷人小祖宗
3楼-- · 2019-01-26 13:17

I had the same error after only renaming a ClassA that uses the NSCoding protocol to ClassB.

For me the problem was that I had used the ClassA in my UserDefaults. So there was the old name ClassA stored in the UserDefaults .plist.

When the Application was starting it tried to read the .plist and because there was a mismatch in the names the Application crashes.

Solution for me was just to delete the user defaults and run the app again and it worked.

To delete the defaults I used this command in terminal:

defaults delete buldle-identifier

查看更多
姐就是有狂的资本
4楼-- · 2019-01-26 13:32

another way is to fix the name of the class used for NSCoding. You simply have to use:

  • NSKeyedArchiver.setClassName("Record", forClass: Record.self before serializing
  • NSKeyedUnarchiver.setClass(Record.self, forClassName: "Record") before deserializing

wherever needed.

Looks like iOS extensions prefix the class name with the extension's name.

查看更多
再贱就再见
5楼-- · 2019-01-26 13:32

In Case if you moved your file to another module, you would need to add additional information

NSKeyedArchiver.setClassName("OldModule.ClassName", for: ClassName.self)
NSKeyedUnarchiver.setClass(ClassName.self, forClassName: "OldModule.ClassName")
查看更多
登录 后发表回答