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]; }
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.
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.
I'm using Mac OS X 10.6.2 and XCode 3.2.1
Storing Default Objects The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs. These methods are described in Setting Default Values.
static func setObject(value:AnyObject ,key:String) { let pref = NSUserDefaults. standardUserDefaults() pref. setObject(value, forKey: key) pref. synchronize() } static func getObject(key:String) -> AnyObject { let pref = NSUserDefaults.
There isn't a way to check whether an object within NSUserDefaults is empty or not. However, you can check whether a value for particular key is nil or not.
To remove a key-value pair from the user's defaults database, you need to invoke removeObject(forKey:) on the UserDefaults instance. Let's update the previous example. The output in the console confirms that the removeObject(forKey:) method works as advertised.
From the documentation for -registerDefaults:
(emphasis added):
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.
I usually use this in -applicationDidFinishLaunching:
:
// Load default defaults [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]];
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.
Edit: Swift 3 variant:
UserDefaults.standard.register(defaults: NSDictionary(contentsOf: Bundle.main.url(forResource: "Defaults", withExtension: "plist")!)! as! [String : Any])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With