Search code examples
iosswiftnskeyedunarchiver

-[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class


I have an error I don't understand after I updated my app. I have an app already in the app store. I have some app specific data archived and stored in a file inside application support directory. I was reading and storing the data every time when the app is launched and sent to background. Everything was fine so far. Now I released an update to the app. After the update when I launch the app and try to unarchive the data it crashes with below exception.

-[NSKeyedUnarchiver decodeObjectForKey:]: 
cannot decode object of class (ProjectName.ProjectFile) for key (root);
the class may be defined in source code or a library that is not linked.

If I delete the app and install the app again everything works fine.

Both my original app and update are written in Swift. I haven't changed the file name also from version 1 to 2. Does any one know what is happening ?


Solution

  • I found the reason for the above.

    Basically I changed the project target name from version 1 to version 2. So it was trying to decode ProjectName2.ProjectFile instead of ProjectName.ProjectFile.

    The possible way to avoid is @objc(PFUserUtils) for the class when you start the first version. If you change the target name like me, use NSKeyedUnarchiver.setClass(className forClassName:name) method to set the class name to old one.