开发者

iOS -- updating data structures in new app version

I am not using core data. Just to keep it simple, let's say I have data which were formerly of type NSString, and now they a开发者_如何学Pythonre supposed to be objects of a custom class Person whose only ivar is a "name" ivar of type NSString. In the updated version of the app, I want my Person objects to have their "name" set to whatever the NSString was in the saved data. But suppose my people appear in lots of different places in the app, so telling it how to handle each one individually would be a pain.

What is the best way to handle this? In particular, is there some trick I can do to catch it in the un-archiving process? Or do I have to go through every un-archived object and turn the appropriate NSStrings into Person objects?


You can create a utility class that check the value that come back from your unarchiver, run it through this method. If the value is an NSString, then you can construct a new Person object, if not, then just returns that object back.

+ (Person *)personFromStringOrPersonObject:(id)object {
    if ([object isKindOfClass:[NSString class]]) {
        // Construct your Person object
        Person person = [Person new];
        person.name = object;
        return person;
    } else {
        return object;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜