将旧Plist与新Plist合并

时间:2012-10-08 09:13:41

标签: objective-c ios ios4

我已向现有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]);
    }
}

提前致谢

2 个答案:

答案 0 :(得分:2)

我会在你的plist中包含一个 version 键,以便将它与应用程序的版本进行比较。您可以使用以下代码段检查应用程序的版本:

 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]

然后,在应用程序启动时:

  1. 读取plist,如果没有 version 键,它将是旧版本
  2. 如果旧版本,则执行数据迁移到当前版本,添加具有默认值的不存在的键。在这里,我会考虑不同的版本号以供将来更新。
  3. 使用捆绑包的版本添加版本键作为值。
  4. 保存plist。

答案 1 :(得分:0)

你可以这样做:

  1. 重命名旧plist,例如old.plist
  2. 从单独的文件中读取两个plist
  3. 合并所需的信息,以编程方式将所选信息添加到新plist
  4. 保存新plist合并
相关问题