NSKeyedUnarchiver error after renaming Xcode proje

2019-01-26 12:35发布

问题:

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?

回答1:

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.

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



回答2:

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.



回答3:

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")


回答4:

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