Re: storing default preferences
Re: storing default preferences
- Subject: Re: storing default preferences
- From: "b.bum" <email@hidden>
- Date: Sun, 14 Mar 2004 13:51:30 -0800
On Mar 13, 2004, at 10:21 PM, cauri wrote:
>
My application has a number of lists that users can customise. I
>
manage these lists through the preferences panel. On first run (or on
>
the click of a "restore defaults" button) I would like these arrays to
>
populate with the default data. What is the best way to store this
>
data? In an XML file, a tabbed text file, or does some other mechanism
>
exist in cocoa? And what is the most efficient way to retrieve this
>
data?
As Mmalcolm mentioned, this is covered in the documentation.
Specifically, it mentions that you would add something like the
following to, say, your App Delegate (or some other class that is
referenced very early during app launch).
+ (void)initialize{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary
dictionaryWithObject:@"YES" forKey:@"DeleteBackup"];
[defaults registerDefaults:appDefaults];
}
I have extended the above pattern to something like the following that
I have used for years. It has the advantage of pushing the default
values out to an external file and, as such, managing the "default"
default settings is a matter of just editing the one file. For Cocoa
apps, I like to do this in -applicationWillFinishLaunching: on my
application delegate:
- (void) applicationWillFinishLaunching: (NSNotification *)
aNotification
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *registrationDefaultsPath = [[NSBundle mainBundle]
pathForResource: @"RegistrationDefaults" ofType: @"plist"];
NSDictionary *registrationDefaults = [NSDictionary
dictionaryWithContentsOfFile: registrationDefaultsPath];
[defaults registerDefaults: registrationDefaults];
}
Then, I create the file named "RegistrationDefaults.plist" and push all
of the default values into it. I.e. the above example would be
written as:
{
"DeleteBackup" = YES;
}
The one downside is that you can't directly push values into the file
that cannot be represented in a standard plist. In that case, I will
use whatever encoding/decoding scheme I'm otherwise using to push 'em
in/out of the defaults database to effectively NSLog() the encoded
value that is then copy/pasted into the file. A bit roundabout,
admittedly, but better-- IMHO-- than having a huge hardwired
NSDictionary stuck in code.
Another advantage to this approach is that I can easily open the app
wrapper and see exactly what the "default" default values are w/o doing
some kind of a nasty runtime hacque.
b.bum
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.