 |
 |
NSUserDefaults
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
What I want to do is to write some preferences to disk and then read them and use them the next time my program launches. If the preference file doesn't exist when I try to read them at startup I want to use the program's defaults and only create a preference file if the user changes the settings.
Well Apple's documentation is kind of confusing. I can see that I want to use the Application's domain, unfortunately I'm not sure what my applications domain is. It said something about a bundle type but I thought that a bundle type was the 4 character creator code and that doesn't really make sense. I have found that in the init method I can write something like:
[[NSUserDefaults standardUserDefaults] setInteger:8
forKey:@"height"];
In order to save the number 8 to a key named height. It is some sort of incorporeal thing that doesn't represent a file buyt can be read from while my program is executing. How to I write to / read from a file and what is up with my applications domain?
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
What you are doing is correct. By default, the standardUserDefaults is a chain of defaults domains which includes your application's domain. When you read values, they will be searched in this chain (starting with the app domain); when you write values, they will be written to your app domain. When you quit, or explicitly synchronize, the values will be written out to disk.
You can find preferences stored in ~/Library/Preferences as xml property list files.
The name that is used for your domain is the CFBundleIdentifier which you set in PB (and it ends up in the app's Info.plist file). You should use java style domain identifiers for this, as it should be unique --- for instance, com.apple.TextEdit, etc.
One word of caution: On the Mac, in the past, some apps have stored huge amounts of data in the preferences folder (for instance, internet caches). The NSUserDefaults / CFPreferences mechanism is not appropriate for this as handling of megabytes of data in xml files is not efficient.
But don't take this the wrong way --- NSUserDefaults / CFPreferences is appropriate in many common cases involving structured data, such as storing collections (arrays, dictionaries) of various data types (strings, numbers, dates, ...) . You can check out the existing domains on your machine for instance. (Use the "defaults" command line app or the PropertyListEditor app, for browsing...)
Ali
[This message has been edited by ali (edited 01-24-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Okay, I can see that in order to READ a setting from my preferences when they are saved, I don't have to specify any domain because the domains will be searched systematically starting with my application domain. Now, where I am stuck is writing to a specific domain.
What I want to do, is to make my program initialize all of the settings in the NSRegistrationDomain, at program launch or, if a preference file already exists, use what is in the preference file instead. Since NSUserDefaults checks the application's domain before any others I thought that a good way to do this would be to put in the awakeFromNib or init method something that'll write all of the default preferences to the NSRegistrationDomain. Then, whenever I read a default setting, it'll read it from the NSRegistrationDomain, if there is no preference file or no entry in a preference file, or it'll read it from the preference file if there is one which has a corresponding entry. When I save changes to preferences I'd write them only to the application's domain.
Now, how to I write to a specific domain? There doesn't seem to be an appropriate method to do so. I only have something like 5 integers that I want to store in the preferences, plus the window location, so I don't have to go to the trouble of making my own proprietary preference system in order to be efficient, although, if I can't figure this out soon, that's exactly what I'll do.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
It is possible to set values for a particular domain (-setPersistentDomain:forName: and -setVolatileDomain:forName  but those should only be needed in extreme circumstances.
To use the defaults system, you want to register some basic settings that should be used if not otherwise set by the user. Simply use the registerDefaults: method to add a set of defaults to the registration domain. [I typically stick to using NSString values only there, though it's possible NSNumbers/NSDates work too.]
To get a value out, use the -stringForKey: method, or one of the conveniences like -intForKey:, -boolForKey:, etc. That will get the user's setting, or the registered default if the user hasn't set anything. [If there was no registered default, then you'll get nil, or NO, or 0, etc.]
To set a value, use the setObject:forKey: or one of the conveniences. This will do the right thing.
In short, don't worry about the individual domains too much. Just use registerDefaults: for the default values, -stringForKey: (or equivalent) when getting the current setting, and setObject:forKey: (or equivalent) when changing the user's setting.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|