简体   繁体   English

为什么要使用registerDefaults:而不是setValue:forKey :?

[英]Why use registerDefaults: instead of setValue:forKey:?

When I'm setting up the default preferences for my app, I'm doing the following: 在为我的应用设置默认首选项时,我将执行以下操作:

1) Reading Root.plist from inside Settings.bundle into a dictionary. 1)从Settings.bundle内部将Root.plist读入字典。

2) I test if a preference is set, and if not I'm registering my defaults dictionary via [NSUserDefaults standardUserDefaults] registerDefaults:] 2)我测试是否设置了首选项,如果没有设置,我将通过[NSUserDefaults standardUserDefaults] registerDefaults:]注册我的默认字典[NSUserDefaults standardUserDefaults] registerDefaults:]

Problem is, defaults registered with registerDefaults: do not go to the persistent store, so if the user never changes their preferences I am reading my default preferences and registering them with NSUserDefaults every time the app launches. 问题是,使用registerDefaults:默认设置registerDefaults:不会进入持久性存储,因此,如果用户从不更改其首选项,则我将读取我的默认首选项,并在每次应用启动时向NSUserDefaults注册它们。

Instead of using registerDefaults: I could use setValue:forKey: and have my default setting go to the persistent store, bypassing the need to build & register a dictionary on each launch. 而不是使用registerDefaults:我可以使用setValue:forKey:并将我的默认设置转到持久性存储中,而无需在每次启动时构建和注册字典。 However, Apple's documentation and sample code both point to registerDefaults: . 但是,Apple的文档示例代码都指向registerDefaults:

So I'm trying to figure out when and why I should use registerDefaults: and when/why I should use setValue:forKey: instead? 因此,我试图弄清楚何时以及为什么应该使用registerDefaults:以及何时/为什么应该使用setValue:forKey:呢?

Problem is, defaults registered with registerDefaults: do not go to the persistent store, so if the user never changes their preferences I am reading my default preferences and registering them with NSUserDefaults every time the app launches. 问题是,使用registerDefaults注册的默认设置:不会进入持久性存储,因此,如果用户从不更改其首选项,则我将读取我的默认首选项,并在每次应用启动时使用NSUserDefaults注册它们。

Yes. 是。 Why is that a problem? 为什么会有问题呢? Why write to disk things you have in the code already? 为什么要将代码中已有的东西写入磁盘?

Instead of using registerDefaults: I could use setValue:forKey: and have my default setting go to the persistent store, bypassing the need to build & register a dictionary on each launch. 而不是使用registerDefaults:我可以使用setValue:forKey:并将我的默认设置转到持久性存储中,而无需在每次启动时构建和注册字典。 However, Apple's documentation and sample code both point to registerDefaults:. 但是,Apple的文档和示例代码都指向registerDefaults:。

Only if you first checked "is this value set? No? OK, now set it." 仅当您首先选中“是否设置了此值?否?确定,现在进行设置”。 That's more code and cost than just using registerDefaults: . 这不仅仅是使用registerDefaults:代码和成本。

You should use registerDefaults: to set the defaults. 您应该使用registerDefaults:设置默认值。 You should use setValue:forKey: to save values that are actively set. 您应该使用setValue:forKey:保存活动设置的值。

Remember also that NSUserDefaults exists on Mac as well. 还要记住, NSUserDefaults也存在于Mac上。 There, the user can directly access the settings with the defaults command, so the program is not the only entity that can modify this store. 在那里,用户可以使用defaults命令直接访问设置,因此该程序不是唯一可以修改此存储的实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM