Re: NSUserDefaults key validation
Re: NSUserDefaults key validation
- Subject: Re: NSUserDefaults key validation
- From: Angela Brett <email@hidden>
- Date: Sat, 17 Nov 2001 19:35:35 +1300
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?
You can use registerDefaults: (a NSUserDefaults instance method)
before using the defaults to set the, er... default defaults. If
there is no value set for the key @"ShowHintMessages" then [prefs
boolForKey:@"ShowHintMessages"] will return whatever you told it to
in the registerDefaults: call. If there is a value set, it will
return that.
From the docs:
NSRegistrationDomain
The registration domain is a set of application-provided defaults
that are used unless a user
overrides them. For example, the first time you run Project Builder,
there isn't an IndexOnOpen
value saved in your defaults database. Consequently, Project Builder
registers a default value for
IndexOnOpen in the NSRegistrationDomain as a "catch all" value.
Project Builder can thereafter
assume that an NSUserDefaults object always has a value to return for
the default, simplifying
the use of user defaults.
You set NSRegistrationDomain defaults programmatically with the
method registerDefaults:.
--
Angela Brett email@hidden
http://acronyms.co.nz/angela
"Great minds think different."