iosswiftnscodingnskeyedunarchiver

How can I decode an object when original class is not available?


I have an iOS 7 application that saves a custom object to app's iCloud Docs folder as a file. For this, I make use of NSCoding protocol.

@interface Person : NSObject <NSCoding>

    @property (copy, nonatomic) NSString *name
    @property (copy, nonatomic) NSString *lastName

@end

Object serialization works perfectly in iOS 7 version of the app:

  1. initWithCoder and encodeWithCoder

  2. [NSKeyedArchiver archivedDataWithRootObject:person]

  3. person = NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)theData]

But I need to move this app to iOS 8, and this class will be coded in swift and 'renamed' for this new iOS 8 version of the app.

class PersonOldVersion: NSObject, NSCoding {
    var name = ""
    var lastName = ""
}

When I try to unarchive the object I got the following error:

*** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (Person)'

I already tried renaming swift class 'PersonOldVersion' to original class name ('Person') but still fails.

How can I decode an object which original class isn't available?


Solution

  • If the name of the class in Objective-C is important, you need to explicitly specify the name. Otherwise, Swift will provide some mangled name.

    @objc(Person)
    class PersonOldVersion: NSObject, NSCoding {
        var name = ""
        var lastName = ""
    }