NSUserDefaults intitial setup

巧了我就是萌 提交于 2019-12-24 19:23:47

问题


I'm saving a few user settings in NSUserDefaults. If these settings have not been modified by the user, I'd like the app to use presets.

How should I set this up so the app first checks if settings have been modified and if not, sets presets?

Checking if [[NSUserDefaults standardDefaults]objectForKey: @"setting1"] == nil ? Then setting a preset if it is not?


回答1:


[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html#//apple_ref/doc/uid/20000318-CIHDDCDB

Call that from your applicationDidFinishLaunchingWithOptions: method that sets up whatever defaults you may need.




回答2:


Look at the NSUserDefaults docs for registerDefaults. This allows you to have default values until the user changes them, then they have no effect.



来源:https://stackoverflow.com/questions/11870648/nsuserdefaults-intitial-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!