Re: Preferences using NSUserDefaults (newbie)
Re: Preferences using NSUserDefaults (newbie)
- Subject: Re: Preferences using NSUserDefaults (newbie)
- From: Eric Peyton <email@hidden>
- Date: Thu, 21 Jun 2001 13:46:24 -0500
On Thursday, June 21, 2001, at 05:10 AM, Jorge Salvador Caffarena
wrote:
Hello,
I am adding preferences support to my application using
NSUserDefaults. I looked at the tutorial found in
cocoadevcentral.com, but it do not work in my app.
For example they say that you can use this to check if the
preferences file exists:
if (! [NSUserDefaults standardUserDefaults] )
{
// create the default preferences
}
But the condition never evaluates to false, even if there are no
preferences file (a plist I suppose).
This will always return an object (hence true). You are not
getting the preferences from this call, but the object that
controls the preferences for your application. Their example may
be flawed ... One good thing to do is forget about checking to see
if the preferences are initialized. Just go ahead and register
your defaults. If the preferences are already there they will
overwrite the defaults you initialize and if the preferences are
not there they will use the ones you register.
I generally do something like this at the start of one of my main
controllers
+ (void)initialize
{
id registrationDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"0", @"SomePrefNameIWantOffByDefault",
@"1", @"SomePrefNameIWantOnByDefault",
nil ];
[[NSUserDefaults standardUserDefaults]
registerDefaults:registrationDict];
}
Then later in my application I do
[[NSUserDefaults standardUserDefaults]
boolForKey:@"SomePrefNameIWantOffByDefault"]; to get the value.
If the user has at some point saved their preferences with a 1
instead of my 0, the 1 will be returned and not my registered
default of 0.
Happily - through the power of foundation kit this all does the
"right" thing, translating the "0" into a 0 and resulting in a NO.
I'm sure you could use an NSNumber in place of the @"0", but if you
are sure that it'll do the right thing (and in this case it does
appear to) then use it.
Also they show some code to initialize the preferences for the
first time, and they use this:
// create a dictionary
NSMutableDictionary *defaultPrefs = [NSMutableDictionary dictionary];
// put default prefs in the dictionary
[defaultPrefs setBool: YES forKey: somePrefOnOrOff];
// register the dictionary of defaults
[preferences registerDefaults: defaultPrefs];
NSLog(@"Registered defaults: %@", defaultValues);
I have no idea where they came up with this ...
But the compiler complaints here and says that
"NSMutableDictionary do not respond to setBool:forKey message".
So now I am really messed up, because this seems a easy and quick
way of implementing preferences, but I am unable to get it to work.
NSUserDefaults is a quick and easy way. I suggest that you read
the documentation on NSUserDefaults and then look at an application
(TextEdit or Sketch are good ones and you have their source
already) that implements the preferences correctly.
Eric
Thanks in advance,
Jorge Salvador Caffarena
http://homepage.mac.com/eevyl/
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev