Wednesday, May 29, 2002

Recently, I have posted a number of rants and references, but I haven't posted any code! That goes against the charter of my weblog. So, time for a chunk of code.

This handy little idiom assumes that you have added a file by the name 'RegistrationDefaults.plist' as a resource in your Cocoa based application project (with a slight modification, it can be used in bundles, frameworks, and-- with a bit of porting-- WebObjects, too).

The contents of the file are read as a property list (on OS X, see the property list DTD for the XML definition) and the contents are registered with the user defaults manager as the baseline set of defaults. The -registrationDefaults: method applies the defaults into the application's defaults domain as a sort of last resort place to find defaults. That is, any default values provided on the command line or written into the user's defaults database (files in ~/Library/Preferences/) will override the registration defaults.

... application will/did finish launching notification handler ...

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *registrationDefaultsPath = [[NSBundle mainBundle] pathForResource: @"RegistrationDefaults" ofType: @"plist"];
NSDictionary *registrationDefaults = [[NSString stringWithContentsOfFile: registrationDefaultsPath] propertyList];
[standardUserDefaults registerDefaults: registrationDefaults];


Simple, huh?

The result is extremely useful. As the developer, it means that you can adjust the baseline set of defaults advertised by your application simply by editing the "ReigstrationDefaults.plist" file. By putting all defaults into this file, you never have to worry about retrieving a NULL value from calls like -stringValueForKey: on NSUserDefaults. If the user deletes the app preferences file or removes a particular default, your application will always fall back to the registration defaults.

For the crafty user that pokes around your app's wrapper, they can easily discover all of your app's defaults without having to poke about the binary.

(NSDictionary implements +dictionaryWithContentsOfFile:, but NSString's -propertyList has traditionally emitted much better parse errors, if needed. I haven't compared the two in OS X.)
8:52:15 PM    

Through the kind help of Carl Lindberg and others, I figured out how to install both NeXTSTEP 3.3 and OpenStep 4.2 under Virtual PC. I used Omni Outliner to take notes of the entire process. Maybe someone will find them useful. I'm sure someone will find errors or have suggestions; send me email if you do. I failed to install Rhapsody DR2 successfully (Thanks to Christian Brunschen for help getting me as far as I did).
1:51:37 AM