首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并旧Plist和新Plist

合并旧Plist和新Plist
EN

Stack Overflow用户
提问于 2012-10-08 17:13:41
回答 2查看 213关注 0票数 0

我已经向现有的Plist添加了新的实体。由于Plist不一致,新版本的应用程序崩溃。

如何将旧的plist数据与新的plist合并?

我的Plist创建方法如下

代码语言:javascript
复制
- (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]);
    }
}

提前感谢

EN

回答 2

Stack Overflow用户

发布于 2012-10-08 17:36:48

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

代码语言:javascript
复制
 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]

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

  1. 读取plist,如果没有version key,则为旧版本。
  2. 如果是旧版本,则执行数据迁移到当前版本,添加不存在的key。在这里,我会在将来的更新中考虑不同的版本号。
  3. 添加版本密钥,并将捆绑包的版本作为值。
  4. 保存plist。
票数 2
EN

Stack Overflow用户

发布于 2012-10-08 17:17:31

你可以这样做:

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

>G29

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12778618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档