NSKeyedUnarchiver error after renaming Xcode project
Asked Answered
D

6

9

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?

Dextrin answered 13/11, 2015 at 21:38 Comment(6)
Does Buck Tracker conform to the NSCoding protocol?Encyclopedia
have you tried to do a 'clean' ?Flagstad
@hal9000 I did but it didn't workDextrin
@Encyclopedia What should I do to conform it to NSCoding?Dextrin
Do you have existing users with stored data that you want to preserve access to?Animalism
@robmayoff yeah in fact reseting the simulator works but this app is already on App Store so I can't do thatDextrin
A
5

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.

Animalism answered 13/11, 2015 at 21:58 Comment(2)
Wow you are life saver! Didn't know I can do that :DDextrin
How do you do this if you want to change the name not just on the display name?Simar
D
19

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.

Drennen answered 11/5, 2016 at 12:36 Comment(1)
I spent half a day on this. Thank you!Abscission
M
10

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")
Munificent answered 29/8, 2017 at 15:24 Comment(1)
This is the correct answer when modules changed or target was copied etc. Thanks man.Pinzler
A
5

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.

Animalism answered 13/11, 2015 at 21:58 Comment(2)
Wow you are life saver! Didn't know I can do that :DDextrin
How do you do this if you want to change the name not just on the display name?Simar
S
2

Just delete the old app and run it again works for me

Supergalaxy answered 9/9, 2020 at 6:59 Comment(0)
S
0

I had this issue last night. Sorted it after 3 hours investigating by simply changing the 'Product Name' (under Build Settings) back to the original value.

Saintpierre answered 30/1, 2020 at 15:46 Comment(0)
M
-1

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

Mather answered 28/10, 2018 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.