Contents
  1. 1. The usage of registerDefaults

Here is the answer I made on SO about registerDefaults

[code language=”objc”]
[[NSUserDefaults standardUserDefaults] setObject:@”Entropy” forKey:@”kName”]
[/code]
will save “Entropy” to a file named com.example.Demo.plist in Library/Preference folder (where com.example.Demo is your Bundle ID, see IOS Application Security Part 20 – Local Data Storage )

[code language=”objc”]
[[NSUserDefaults standardUserDefaults] setObject:@”Mac OS X” forKey:@”kOS”];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
@”Windows”, @”kOS”,
@”Google”, @”kSearchEngine”, nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];

NSLog(@”%@”, [[NSUserDefaults standardUserDefaults] objectForKey:@”kOS”]);
NSLog(@”%@”, [[NSUserDefaults standardUserDefaults] objectForKey:@”kSearchEngine”]);
NSLog(@”%@”, [[NSUserDefaults standardUserDefaults] objectForKey:@”kBrowser”]);
[/code]
will print “Mac OS X”, “Google”, (null)

In fact, registerDefaults

does not save to disk
only sets value for keys that haven’t been set (“kOS” is set by setObject:forKey: and “kSearchEngine” is not set)
returns 0 for scalar values, nil for objects if that key is not set by both registerDefaults and setObject:forKey: (“kBrowser” in this case)

The usage of registerDefaults

Quoted from Preferences and Settings Programming Guide

If these standard default values are not appropriate for your app, you can register your own default values using the registerDefaults: method. This method places your custom default values in the NSRegistrationDomain domain, which causes them to be returned when a preference is not explicitly set.

Quoted from How to Save Data with NSUserDefaults

Another tip is that you can initialize your NSUserDefaults with a pre-defined NSDictionary object. So for example you could set a default value to be “false” or “true” before the user ever had a chance to interact with your program. In my case, sometimes I create an array that represents all the levels in my game, and in each array value I store a boolean to check if a player has finished the level. To do this I create the data object and then register it with NSUserDefaults. If a previous value exists for the object, then nothing happens. Otherwise my blank object gets saved as the “default” defaults
Contents
  1. 1. The usage of registerDefaults