Up

NSUserDefaults class reference

Authors

Georg Tuparev (Tuparev@EMBL-Heidelberg.de)
Richard Frith-Macdonald (rfm@gnu.org)

Version: 1.114

Date: 2004/07/29 15:30:47

Copyright: (C) 1995-2001 Free Software Foundation, Inc.

Software documentation for the NSUserDefaults class

NSUserDefaults : NSObject

Declared in:
Foundation/NSUserDefaults.h
Standards:

NSUserDefaults provides an interface to the defaults system, which allows an application access to global and/or application specific defualts set by the user. A particular instance of NSUserDefaults, standardUserDefaults, is provided as a convenience. Most of the information described below pertains to the standardUserDefaults. It is unlikely that you would want to instantiate your own userDefaults object, since it would not be set up in the same way as the standardUserDefaults.

Defaults are managed based on domains. Certain domains, such as NSGlobalDomain, are persistant. These domains have defaults that are stored externally. Other domains are volitale. The defaults in these domains remain in effect only during the existance of the application and may in fact be different for applications running at the same time. When asking for a default value from standardUserDefaults, NSUserDefaults looks through the various domains in a particular order.

NSArgumentDomain... volatile
Contains defaults read from the arguments provided to the application at startup.
Application (name of the current process)... persistent
Contains application specific defaults, such as window positions.
NSGlobalDomain... persistent
Global defaults applicable to all applications.
Language (name based on users's language)... volatile
Constants that help with localization to the users's language.
NSRegistrationDomain... volatile
Temporary defaults set up by the application.

The NSLanguages default value is used to set up the constants for localization. GNUstep will also look for the LANGUAGES environment variable if it is not set in the defaults system. If it exists, it consists of an array of languages that the user prefers. At least one of the languages should have a corresponding localization file (typically located in the Languages directory of the GNUstep resources).

As a special extension, on systems that support locales (e.g. GNU/Linux and Solaris), GNUstep will use information from the user specified locale, if the NSLanguages default value is not found. Typically the locale is specified in the environment with the LANG environment variable.

The first change to a persistent domain after a -synchronize will cause an NSUserDefaultsDidChangeNotification to be posted (as will any change caused by reading new values from disk), so your application can keep track of changes made to the defaults by other software.

NB. The GNUstep implementation differs from the Apple one in that it is thread-safe while Apple's (as of MacOS-X 10.1) is not.

Method summary

resetStandardUserDefaults 

+ (void) resetStandardUserDefaults;

Resets the shared user defaults object to reflect the current user ID. Needed by setuid processes which change the user they are running as.
In GNUstep you should call GSSetUserName() when changing your effective user ID, and that class will call this function for you.


setUserLanguages: 

+ (void) setUserLanguages: (NSArray*)languages;

Sets the array of user languages preferences. Places the specified array in the NSLanguages user default.


standardUserDefaults 

+ (NSUserDefaults*) standardUserDefaults;

Returns the shared defaults object. If it doesn't exist yet, it's created. The defaults are initialized for the current user. The search list is guaranteed to be standard only the first time this method is invoked. The shared instance is provided as a convenience; other instances may also be created.


userLanguages 

+ (NSArray*) userLanguages;

Returns the array of user languages preferences. Uses the NSLanguages user default if available, otherwise tries to infer setup from operating system information etc (in particular, uses the LANGUAGES environment variable).


addSuiteNamed: 

- (void) addSuiteNamed: (NSString*)aName;

Adds the domain names aName to the search list of the receiver.
The domain is added after the application domain.
Suites may be removed using the -removeSuiteNamed: method.


arrayForKey: 

- (NSArray*) arrayForKey: (NSString*)defaultName;

Looks up a value for a specified default using -objectForKey: and checks that it is an NSArray object. Returns nil if it is not.


boolForKey: 

- (BOOL) boolForKey: (NSString*)defaultName;

Looks up a value for a specified default using -objectForKey: and returns its boolean representation.
Returns NO if it is not a boolean.
The text 'yes' or 'true' or any non zero numeric value is considered to be a boolean YES. Other string values are NO.
NB. This differs slightly from the documented behavior for MacOS-X (August 2002) in that the GNUstep version accepts the string 'TRUE' as equivalent to 'YES'.


dataForKey: 

- (NSData*) dataForKey: (NSString*)defaultName;

Looks up a value for a specified default using -objectForKey: and checks that it is an NSData object. Returns nil if it is not.


dictionaryForKey: 

- (NSDictionary*) dictionaryForKey: (NSString*)defaultName;

Looks up a value for a specified default using -objectForKey: and checks that it is an NSDictionary object. Returns nil if it is not.


dictionaryRepresentation 

- (NSDictionary*) dictionaryRepresentation;

Returns a dictionary representing the current state of the defaults system... this is a merged version of all the domains in the search list.


floatForKey: 

- (float) floatForKey: (NSString*)defaultName;

Looks up a value for a specified default using -objectForKey: and checks that it is a float. Returns 0.0 if it is not.


init 

- (id) init;

Initializes defaults for current user calling initWithUser:


initWithContentsOfFile: 

- (id) initWithContentsOfFile: (NSString*)path;
This is a designated initialiser for the class.

Initializes defaults for the specified path. Returns an object with an empty search list.


initWithUser: 

- (id) initWithUser: (NSString*)userName;

Initializes defaults for the specified user calling -initWithContentsOfFile:


integerForKey: 

- (int) integerForKey: (NSString*)defaultName;

Looks up a value for a specified default using -objectForKey: and checks that it is an integer. Returns 0 if it is not.


objectForKey: 

- (id) objectForKey: (NSString*)defaultName;

Looks up a value for a specified default using. The lookup is performed by accessing the domains in the order given in the search list.
Returns nil if defaultName cannot be found.


persistentDomainForName: 

- (NSDictionary*) persistentDomainForName: (NSString*)domainName;

Returns the persistent domain specified by domainName.


persistentDomainNames 

- (NSArray*) persistentDomainNames;

Returns an array listing the name of all the persistent domains.


registerDefaults: 

- (void) registerDefaults: (NSDictionary*)newVals;

Merges the contents of the dictionary newVals into the registration domain. Registration defaults may be added to or replaced using this method, but may never be removed. Thus, setting registration defaults at any point in your program guarantees that the defaults will be available thereafter.


removeObjectForKey: 

- (void) removeObjectForKey: (NSString*)defaultName;

Removes the default with the specified name from the application domain.


removePersistentDomainForName: 

- (void) removePersistentDomainForName: (NSString*)domainName;

Removes the persistent domain specified by domainName from the user defaults.
Causes a NSUserDefaultsDidChangeNotification to be posted if this is the first change to a persistent-domain since the last -synchronize .


removeSuiteNamed: 

- (void) removeSuiteNamed: (NSString*)aName;

Removes the named domain from the serach list of the receiver.
Suites may be added using the -addSuiteNamed: method.


removeVolatileDomainForName: 

- (void) removeVolatileDomainForName: (NSString*)domainName;

Removes the volatile domain specified by domainName from the user defaults.


searchList 

- (NSMutableArray*) searchList;

Returns an array listing the domains searched in order to look up a value in the defaults system. The order of the names in the array is the order in which the domains are searched.


setBool: forKey: 

- (void) setBool: (BOOL)value forKey: (NSString*)defaultName;

Sets a boolean value for defaultName in the application domain.
The boolean value is stored as a string - either YES or NO. Calls -setObject:forKey: to make the change.


setFloat: forKey: 

- (void) setFloat: (float)value forKey: (NSString*)defaultName;

Sets a float value for defaultName in the application domain.
Calls -setObject:forKey: to make the change.


setInteger: forKey: 

- (void) setInteger: (int)value forKey: (NSString*)defaultName;

Sets an integer value for defaultName in the application domain.
Calls -setObject:forKey: to make the change.


setObject: forKey: 

- (void) setObject: (id)value forKey: (NSString*)defaultName;

Sets an object value for defaultName in the application domain.
The defaultName must be a non-empty string.
The value must be an instance of one of the [NSString -propertyList] classes.

Causes a NSUserDefaultsDidChangeNotification to be posted if this is the first change to a persistent-domain since the last -synchronize .


setPersistentDomain: forName: 

- (void) setPersistentDomain: (NSDictionary*)domain forName: (NSString*)domainName;

Replaces the persistent-domain specified by domainName with domain... a dictionary containing keys and defaults values.
Raises an NSInvalidArgumentException if domainName already exists as a volatile-domain.
Causes a NSUserDefaultsDidChangeNotification to be posted if this is the first change to a persistent-domain since the last -synchronize .


setSearchList: 

- (void) setSearchList: (NSArray*)newList;

Sets the list of the domains searched in order to look up a value in the defaults system. The order of the names in the array is the order in which the domains are searched.
On lookup, the first match is used.


setVolatileDomain: forName: 

- (void) setVolatileDomain: (NSDictionary*)domain forName: (NSString*)domainName;

Sets the volatile-domain specified by domainName to domain... a dictionary containing keys and defaults values.
Raises an NSInvalidArgumentException if domainName already exists as either a volatile-domain or a persistent-domain.


stringArrayForKey: 

- (NSArray*) stringArrayForKey: (NSString*)defaultName;

Calls -arrayForKey: to get an array value for defaultName and checks that the array contents are string objects... if not, returns nil.


stringForKey: 

- (NSString*) stringForKey: (NSString*)defaultName;

Looks up a value for a specified default using -objectForKey: and checks that it is an NSString. Returns nil if it is not.


synchronize 

- (BOOL) synchronize;

Ensures that the in-memory and on-disk representations of the defaults are in sync. You may call this yourself, but probably don't need to since it is invoked at intervals whenever a runloop is running.
If any persistent domain is changed by reading new values from disk, an NSUserDefaultsDidChangeNotification is posted.


volatileDomainForName: 

- (NSDictionary*) volatileDomainForName: (NSString*)domainName;

Returns the volatile domain specified by domainName.


volatileDomainNames 

- (NSArray*) volatileDomainNames;

Returns an array listing the name of all the volatile domains.



Up