Defaults are grouped in domains. For example, there’s a domain for application-specific defaults and another for system-wide defaults that apply to all applications. All defaults are stored and accessed per user. Defaults that affect all users are not provided for.
Domains
Argument Domain
Application Domain
Global Domain
Languages Domain
Registration Domain
Defaults are grouped in domains. Each domain has a name by which it’s identified and stores defaults as key-value pairs in an NSDictionary
object. Each default is made up of three components:
The domain in which the default is stored
The name by which the default is identified (an NSString
object)
The default’s value, which can be any property-list object (NSData
, NSString
, NSNumber
, NSDate
, NSArray
, or NSDictionary
)
A domain is either persistent or volatile. Persistent domains are permanent and last past the life of the user defaults object. Persistent domains are stored in a user’s defaults database. If you use a user defaults object (an instance of NSUserDefaults
or NSUserDefaultsController
) to make a change to a default in a persistent domain, the changes are saved in the user’s defaults database automatically. On the other hand, volatile domains last only as long as the user defaults object exists; they aren’t saved in the user’s defaults database. The standard domains are:
Domain |
State |
---|---|
volatile |
|
Application (Identified by the application’s identifier) |
persistent |
persistent |
|
Languages (Identified by the language names) |
volatile |
volatile |
A search for the value of a given default proceeds through the domains in an NSUserDefaults
object’s search list. Only domains in the search list are searched. The standard search list contains the domains from the table above, in the order listed. A search ends when the default is found. Thus, if multiple domains contain the same default, only the domain nearest the beginning of the search list provides the default’s value.
The following sections describe the purpose of each of the domains.
Default values can be set from command line arguments (if you start the application from the command line) as well as from a user's defaults database. Default values set from the command line go in the NSArgumentDomain
. They are set on the command line by preceding the default name with a hyphen and following it with a value. For example, the following command launches Xcode and sets Xcode’s IndexOnOpen
default to NO
:
localhost> Xcode.app/Contents/MacOS/Xcode -IndexOnOpen NO |
Defaults set from the command line temporarily override values from a user’s defaults database. In the example above, Xcode won’t automatically index projects even if the user’s IndexOnOpen
preference is set to YES
in the defaults database.
The application domain contains application-specific defaults that are read from a user’s defaults database. The application domain is identified by the bundle identifier of the application. When you save a user’s defaults in your application, this is typically the domain to which the values are saved. Values themselves are stored in a file managed by the system. Currently, they are stored in $HOME/Library/Preferences/<ApplicationIdentifier>.plist
; it may be useful to inspect this file for debugging purposes, but you should not modify this file directly—instead you should use the appropriate API provided by the user defaults objects.
The global domain contains defaults that are read from a user’s defaults database and are applicable to all applications that a user runs. Many Application Kit and Foundation objects use default values from the NSGlobalDomain
. For example, NSRulerView
objects automatically use a user’s preferred measurement units, as stored in the user’s defaults database under the key AppleMeasurementUnits
. Consequently, ruler views in all applications use the user’s preferred measurement units—unless an application overrides the default by creating an AppleMeasurementUnits
default in its application domain. Another NSGlobalDomain
default, under the key AppleLanguages
, allows users to specify a preference of languages as an array of strings. For example, a user could specify English as the preferred language, followed by Spanish, French, German, Italian, and Swedish.
If a user has a value for the AppleLanguages
default, then NSUserDefaults
records language-specific default values in domains identified by the language name. The language specific domains contain defaults for a locale. Certain classes from the Foundation Framework (such as NSCalendarDate
, NSDate
, and NSTimeZone
, NSString
, and NSScanner
) use locale defaults to modify their behavior. For example, when you request an NSString
representation of an NSCalendarDate
, the NSCalendarDate
object looks at the locale to determine what the months and the days of the week are named in your preferred language.
The registration domain is a set of application-provided defaults that are used unless a user overrides them (the “default defaults” or “factory settings”). For example, the first time you run Xcode, there isn’t an IndexOnOpen
value saved in your defaults database. Consequently, Xcode registers a default value for IndexOnOpen
in the NSRegistrationDomain
as a “catch all” value. Xcode 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:
.
© 2001, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-10-31)