objective-c - 将旧 Plist 与新 Plist 合并

标签 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]);
    }
}

提前致谢

最佳答案

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

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

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

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

关于objective-c - 将旧 Plist 与新 Plist 合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12778618/

相关文章:

objective-c - iOS 正则表达式 : Targeting currency names

iphone - 如何在 iPhone 中设置此布局需要一些帮助

iphone - NSUnknownKeyException View ...该怎么办?

ios - Messenger 风格的 UITextView 输入

iphone - 在 UILabel 中使用带有粗体文本的自定义字体

ios - RestKit 将未嵌套关系映射到拥有对象

iphone - 我如何在我的 iphone 应用程序中验证 obj c 中的这个简单条件

ios - 2 出现在标注上的 AccessoryItems

ios:如何恢复基于 AVAsset 视频的写作 session

android - 在另一个 : React Native 之上叠加或添加 View