Default Defaults

Written on July 18, 2012 at 11:52 am

One of the first things I do on starting a new AppKit or UIKit project is add a few lines of code to set default values for NSUserDefaults. You’ll never have to check to see if nil was returned from a call to NSUserDefaults once you do this.Add the following snippet to your application delegate’s finished launching method.

1
2
3
4
5
6
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"DefaultDefaults" 
	withExtension:@"plist"];
NSDictionary *d = [NSDictionary dictionaryWithContentsOfURL:url];
if (d)
	[defaults registerDefaults:d];

Then create a property list called “DefaultDefaults.plist” and add it as a resource. Anytime you add another preference/default to your application, add the default value to this plist.