Why use registerDefaults: instead of setValue:forKey:?

后端 未结 1 1470
南笙
南笙 2021-01-01 02:33

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

相关标签:
1条回答
  • 2021-01-01 03:05

    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.

    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. However, Apple's documentation and sample code both point to 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:.

    You should use registerDefaults: to set the defaults. You should use setValue:forKey: to save values that are actively set.

    Remember also that NSUserDefaults exists on Mac as well. 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.

    0 讨论(0)
提交回复
热议问题