|Type||Topic||Plugin||Version||macOS||Windows||Linux||Console & Web||iOS|
|class||Cocoa||MBS MacBase Plugin||10.3||Yes||No||No||Yes, macOS only||No|
dim u as NSUserDefaultsMBS = NSUserDefaultsMBS.standardUserDefaults
u.setStringValue "test", "Hello World"
The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user's preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user's defaults database. The parameters are referred to as defaults since they're commonly used to determine an application's default state at startup or the way it acts by default.
At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user's defaults database. NSUserDefaults caches the information to avoid having to open the user's defaults database each time you need a default value. The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user's defaults database.
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value. For example, if you set a mutable string as the value for "MyStringDefault", the string you later retrieve using stringForKey will be immutable.
A defaults database is created automatically for each user. The NSUserDefaults class does not currently support per-host preferences. To do this, you must use the CFPreferencesMBS class. However, NSUserDefaults correctly reads per-host preferences, so you can safely mix CFPreferencesMBS code with NSUserDefaultsMBS code.
If your application supports managed environments, you can use an NSUserDefaults object to determine which preferences are managed by an administrator for the benefit of the user. Managed environments correspond to computer labs or classrooms where an administrator or teacher may want to configure the systems in a particular way. In these situations, the teacher can establish a set of default preferences and force those preferences on users. If a preference is managed in this manner, applications should prevent users from editing that preference by disabling any appropriate controls.
The NSUserDefaults class is thread-safe.
- 10 properties
- property boolForKey(key as string) as boolean
- property dataForKey(key as string) as memoryblock
- property dictionaryForKey(key as string) as dictionary
- property doubleForKey(key as string) as Double
- property fileForKey(key as string) as folderitem
- property floatForKey(key as string) as single
- property integerForKey(key as string) as Integer
- property stringForKey(key as string) as string
- property URLForKey(key as string) as string
- property variantForKey(key as string) as Variant
- 32 methods
- method addSuiteNamed(suiteName as string)
- method arrayForKey(key as string) as Variant()
- method Constructor
- method Constructor(username as string)
- method dictionaryRepresentation as dictionary
- method objectIsForcedForKey(key as string) as boolean
- method objectIsForcedForKey(key as string, domain as string) as boolean
- method persistentDomainForName(domainName as string) as dictionary
- method persistentDomainNames as string()
- method registerDefaults(dic as dictionary)
- method removeObjectForKey(defaultName as string)
- method removePersistentDomainForName(domainName as string)
- method removeSuiteNamed(suiteName as string)
- method removeVolatileDomainForName(domainName as string)
- method setArrayValue(key as string, values() as Variant)
- method setBoolValue(key as string, value as boolean)
- method setDataValue(key as string, value as memoryblock)
- method setDictionaryValue(key as string, value as dictionary)
- method setDoubleValue(key as string, value as Double)
- method setFileValue(key as string, value as folderitem)
- method setFloatValue(key as string, value as single)
- method setIntegerValue(key as string, value as Integer)
- method setPersistentDomain(domain as dictionary, domainName as string)
- method setStringArrayValue(key as string, values() as string)
- method setStringValue(key as string, value as string)
- method setURLValue(key as string, value as string)
- method setVariantValue(key as string, value as Variant)
- method setVolatileDomain(domain as dictionary, domainName as string)
- method stringArrayForKey(key as string) as string()
- method synchronize as boolean
- method volatileDomainForName(domainName as string) as dictionary
- method volatileDomainNames as string()
- 6 shared methods
This class has no sub classes.
Some examples which use this class:
- Important note for Webkit enabled applications in the Mac App Store
- MBS REALbasic Plugins, version 10.3pr6
The items on this page are in the following plugins: MBS MacBase Plugin.
MBS Xojo Chart Plugins