如何使用 NSUserDefaults 注册用户默认值而不覆盖现有值? [英] How to register user defaults using NSUserDefaults without overwriting existing values?

查看:22
本文介绍了如何使用 NSUserDefaults 注册用户默认值而不覆盖现有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 +(void)initialize 方法的 AppDelegate 类,我用它来注册一些默认值.这是我使用的代码:

I have an AppDelegate class with +(void)initialize method that I use to register some defaults. Here's the code that I use:

+ (void)initialize {
  NSDictionary *defaults = [NSDictionary dictionaryWithObjectsAndKeys:@"NO", @"fooKey", @"YES", @"barKey", nil];

  [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}

我还创建了 Preferences.xib,其中包含显示首选项状态的几个复选框 (NSButton).它们使用相同的键(在本例中为 fooKey 和 barKey)绑定到 NSUserDefaultsController.每次我启动应用并更改默认设置"时,它们都会在下次启动应用时恢复.

I also created Preferences.xib which holds couple of checkboxes (NSButton) that display status of preferences. They are bound to NSUserDefaultsController with same keys (fooKey and barKey in this case). Each time I launch an app and change the "defaults" they are restored on next app launch.

有没有办法在不覆盖现有值的情况下注册默认默认值"?也许每次我构建和启动一个应用程序时,它的首选项文件都会被重新创建?也许我应该从 NSUserDefaultsController 取消绑定复选框,并在首选项窗口控制器中使用一些自定义代码自己维护键的值?

Is there a way to register "default defaults" without overwriting already existing values? Maybe each time I build and launch an app its preferences file is being recreated? Maybe I should unbind checkboxes from NSUserDefaultsController and maintain the values of keys myself with some custom code in preferences window controller?

我想听听您为维护用户默认设置而选择的实现方式.

I'd like to hear your implementation of choice for maintaining user defaults.

我使用的是 Mac OS X 10.6.2 和 XCode 3.2.1

I'm using Mac OS X 10.6.2 and XCode 3.2.1

推荐答案

来自 -registerDefaults: 的文档(重点添加):

From the documentation for -registerDefaults: (emphasis added):

注册域的内容不写入磁盘;每次应用程序启动时都需要调用此方法.您可以将 plist 文件放在应用程序的 Resources 目录中,并使用从该文件中读入的内容调用 registerDefaults:.

The contents of the registration domain are not written to disk; you need to call this method each time your application starts. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.

所以你的代码是在正确的轨道上.这是您注册默认默认值的方式.

So your code was on the right track. This is how you register default defaults.

我通常在 -applicationDidFinishLaunching::

// Load default defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]];

使用 plist 可以轻松地在您的应用中添加和更改默认值,并且可以防止您犯将 @"NO" 用作值的错误.

Using a plist makes it easy to add and change defaults in your app, and it prevents you from making the mistake of using @"NO" as a value too.

Swift 3 变体:

 UserDefaults.standard.register(defaults: NSDictionary(contentsOf: Bundle.main.url(forResource: "Defaults", withExtension: "plist")!)! as! [String : Any])

这篇关于如何使用 NSUserDefaults 注册用户默认值而不覆盖现有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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