O'Reilly logo

Cocoa in a Nutshell by James Duncan Davidson, Michael Beam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

User Defaults

User defaults is another term for user application preferences. Mac OS X has a well-designed user defaults system that is accessed in Cocoa through the Foundation class NSUserDefaults . Working with NSUserDefaults is similar to working with an NSDictionary. Default values are stored in the database by keys that the application developer defines in the application. The defaults database is actually a collection of property list files; every application has its own property list file where defaults are stored. You can view these files in ~/Library/Preferences.

Defaults are organized into domains , which are groupings of default values that have varying degrees of visibility to applications. A domain is either persistent or volatile . Defaults in a persistent domain are stored in the defaults database, while defaults in a volatile domain are applicable only during the lifetime of the NSUserDefaults object that contains those values. NSUserDefaults has five standard domains:

NSArgumentDomain

Set values for defaults in the argument domain by passing key-value pairs to the application as arguments on the command line, (e.g., % MyApp -KeyName Value). The argument domain is volatile, so arguments affect the application only during the application session for which they were specified.

Application

Application-specific defaults are stored here and kept persistently in the user’s defaults database.

NSGlobalDomain

Defaults stored in the global domain are applicable to all applications ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required