Re: domains for NSUserDefaults
Re: domains for NSUserDefaults
- Subject: Re: domains for NSUserDefaults
- From: mmalcolm crawford <email@hidden>
- Date: Sat, 15 Apr 2006 00:10:11 -0700
On Apr 14, 2006, at 11:26 PM, Devraj Mukherjee wrote:
Does any one have an example where the NSUserDefaults is being set for
another domain? I am having real trouble getting my head around this.
You typically don't set defaults in any domain other than the
application domain (when the user sets a preference) and the
registration domain (for "factory settings") -- see <http://
developer.apple.com/documentation/Cocoa/Conceptual/UserDefaults/
Concepts/DefaultsDomains.html> for descriptions of domains and
<http://developer.apple.com/documentation/Cocoa/Conceptual/
UserDefaults/Tasks/UsingDefaults.html> for usage.
To summarise:
When your application starts, you must provide "factory settings" for
all possible defaults values you might ask for -- so that there is a
value there if you ask for it. You do this with
'registerDefaults:'. Rather than set all these programmatically, it
is common to store these as a dictionary in a property list (plist)
file in your applications resources which you read in to pass as the
argument to 'registerDefaults:'.
When you set a user default (with 'setObject:forKey:'),
NSUserDefaults automatically ads the new value to the application
domain. This is then saved in the user's preferences file for your
application. (Note that factory defaults are not saves in the user's
preferences file, as is explained in the next paragraph.) User
defaults are written out automatically at intervals by the system, or
you can force them to be written out by invoking 'synchronize'.
User defaults domains are arranged in a hierarchy (as shown at
<http://developer.apple.com/documentation/Cocoa/Conceptual/
UserDefaults/Concepts/DefaultsDomains.html>). When you ask for a
value for a particular default (with a method such as stringForKey:),
NSUserDefaults searches the domains in order to try to find a value
(and returns the first one found). Because they're arranged in a
hierarchy, in a typical case if a user has set a value it is
retrieved from the Application domain, otherwise it is retrieved from
the factory defaults in the registration domain (where you should
have provided a value...). If you're testing your application, it is
possible you may have provided a commandline argument at launch time
to override any other preferences settings -- commandline preferences
are put in the NSArgumentDomain, which as you will note is the
highest-level domain, and therefore the one NSUserDefaults searches
first...
mmalc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden