Re: NSUserDefaults key validation
Re: NSUserDefaults key validation
- Subject: Re: NSUserDefaults key validation
- From: Peter Ammon <email@hidden>
- Date: Sat, 17 Nov 2001 17:30:08 -0500
On Saturday, November 17, 2001, at 12:20 AM, Dan Huntsinger wrote:
Hi all,
I have a routine that reads in my prefs, and I've got a bug in it. The
following code is an example of what I'm doing:
// load preferences
prefs = [NSUserDefaults standardUserDefaults];
// get show hints pref
// if no prefs have been set, set to default
if( [prefs boolForKey:@"ShowHintMessages"] != nil )
showHints = [prefs boolForKey:@"ShowHintMessages"];
else
showHints = NO;
My problem is this: since the key in this case is a BOOL, if it is set
to
NO, then it will be interpreted as being nil. What I really need is a
method to check if a given key is valid(exists), but I don't see any
such
method in the definition of NSUserDefualts. Does anyone have a
suggestion
or a method they have used in this situation?
Thanks,
Dan
I usually try to set it up so that the default value of nil is the
proper default value for the preference in my app. Barring that, use
objectForKey rather than boolForKey and check if the returned object is
nil.
-Peter