iosxcodeserializationnskeyedarchivernskeyedunarchiver

NSKeyedUnarchiver.unarchiveObjectWithFile crashes when loading classes created from a different target


I have a PLIST file that has been created using production version of the application and added as a resource to the project. The name of the target of production version is "little pony". In order to help with application testing there is another version built though a different target under the same project "little pony beta". At runtime, beta application tries to load PLIST and crashes with message:

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (little pony.Layer) for key (NS.objects); the class may be defined in source code or a library that is not linked'

It seems, that the PLIST should contain little pony beta.Layer instead of little pony.Layer class in order for the loading to succeed.

Is there any way to make NSKeyedUnarchiver.unarchiveObjectWithFile use different namespace for this to work? Of course, I don't want to create two different sets of pre-compiled data, as this is CPU intensive task.


Solution

  • I've found a solution here: https://stackoverflow.com/a/37162921/279813

    The solution proposes to change the name of the class for NSCoding by using

    [NSKeyedUnarchiver setClass:[Layer self] forClassName:@"little pony beta.Layer"];