更新&使用新版本的应用程序更改设置plist文件 [英] Updating & changing settings plist files with new versions of an app

查看:119
本文介绍了更新&使用新版本的应用程序更改设置plist文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用的资源文件夹中有一个默认设置plist文件,并且在第一次启动时会被复制到文档文件夹。

I've got a default settings plist file in the resources folder of my app, and on the first launch that gets copied to the documents folder.

在连续该应用程序的版本,如何将其文档中的plist设置与任何新密钥和&自上一版本以来添加的值(可能是嵌套的)?

In successive versions of the app, how can I merge the plist settings in their documents with any new keys & values (possibly nested) that have been added since the previous version?

我见过一种模式,其中属性实际上是在应用程序中创建为NSDictionary的(默认情况下)设置),然后保存在plist文件中的当前设置与该字典合并,然后保存在当前的plist上。

I've seen a pattern where properties are actually created as an NSDictionary in the app (with all default settings), and then the current settings saved in the plist file is merged with that dictionary, and that is then saved over the current plist.

这是一个好方法吗?如果是这样,你如何合并NSDictionary可能有几个嵌套值与子数组和子词典?

Is that a good approach? If so, how do you go about merging NSDictionary's that could have several nested values with sub arrays and sub dictionaries?

此外,它是否建议有一个单独的自定义plist文件设置,还是应该总是使用NSUserDefaults? NSUserDefaults是否处理版本控制并更改默认值?

Also, is it advised to have a separate custom plist file for settings, or should you always use NSUserDefaults? Does NSUserDefaults handle versioning and changing defaults?

非常感谢,

Mike

推荐答案

好吧,我想我已经想出了最好的方法:

Okay I think I've come up with the best way to do it:

- (void)readSettings {

    // Get Paths
    NSString *defaultSettingsPath = [[NSBundle mainBundle] pathForResource:@"DefaultSettings" ofType:@"plist"];
    NSString *settingsPath = [self.applicationDocumentsPath stringByAppendingPathComponent:@"Settings.plist"];

    // Read in Default settings
    self.settings = [NSMutableDictionary dictionaryWithContentsOfFile:defaultSettingsPath];

    // Read in Current settings and merge
    NSDictionary *currentSettings = [NSDictionary dictionaryWithContentsOfFile:settingsPath];
    for (NSString *key in [currentSettings allKeys]) {
        if ([[self.settings allKeys] indexOfObject:key] != NSNotFound) {
            if (![[currentSettings objectForKey:key] isEqual:[self.settings objectForKey:key]]) {

                // Different so merge
                [self.settings setObject:[currentSettings objectForKey:key] forKey:key];

            }
        }
    }

}

- (void)saveSettings {
    if (self.settings) {
        NSString *settingsPath = [self.applicationDocumentsPath stringByAppendingPathComponent:@"Settings.plist"];
        [self.settings writeToFile:settingsPath atomically:YES];
    }
}

这篇关于更新&使用新版本的应用程序更改设置plist文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆