更新&使用新版本的应用程序更改设置 plist 文件

发布于 2024-08-08 10:45:30 字数 409 浏览 8 评论 0原文

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

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

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

这是一个好方法吗?如果是这样,如何将可能具有多个嵌套值的 NSDictionary 与子数组和子字典合并?

另外,是否建议使用单独的自定义 plist 文件进行设置,还是应该始终使用 NSUserDefaults? NSUserDefaults 是否处理版本控制和更改默认值?

非常感谢,

迈克

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.

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?

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.

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?

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?

Many thanks,

Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江心雾 2024-08-15 10:45:30

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

- (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];
    }
}

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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文