我已将新实体添加到现有 Plist。由于 Plist 不一致,新版本的应用程序崩溃。
如何将旧 plist 数据与新 plist 合并?
我的Plist创建方法如下
- (void)createEditableCopyOfFileIfNeeded:(NSString *)plistName
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:plistName];
success = [fileManager fileExistsAtPath:plistPath];
if (success)
{
return;
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:plistName];
success = [fileManager copyItemAtPath:defaultPath toPath:plistPath error:&error];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
提前致谢
最佳答案
我会在您的 plist 中包含一个 version 键,以将其与应用程序的版本进行比较。您可以使用以下代码段检查应用程序的版本:
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]
然后,在应用程序启动时:
- 读取plist,如果没有version键,就是old version。
- 如果旧版本,则执行数据迁移到当前版本,添加具有默认值的不存在的键。在这里,我会考虑不同的版本号以供将来更新。
- 添加 version 键和您的包的版本作为值。
- 保存 plist。
关于objective-c - 将旧 Plist 与新 Plist 合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12778618/