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

后端 未结 5 793
面向向阳花
面向向阳花 2021-02-05 00:52

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 : NSO         


        
5条回答
  •  死守一世寂寞
    2021-02-05 01:06

    This might be another solution, and it's what I did (since I didn't use Swift at that time).

    In my case, I archived an object of class "City" but then renamed the class to "CityLegacy" because I created a completely new "City" class.

    I had to do this to unarchive the old "City" object as a "CityLegacy" object:

    // Tell the NSKeyedUnarchiver that the class has been renamed
    [NSKeyedUnarchiver setClass:[CityLegacy class] forClassName:@"City"];
    
    // Unarchive the object as usual
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:@"city"];
    CityLegacy *city = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    

提交回复
热议问题