Re: Saving preferences tutorial
Re: Saving preferences tutorial
- Subject: Re: Saving preferences tutorial
- From: "M. Uli Kusterer" <email@hidden>
- Date: Tue, 1 Jul 2003 13:25:27 +0200
At 19:04 Uhr -0600 30.06.2003, Dave Thorup wrote:
<Warning - typed in Mail, use at your own risk>
-----------------------
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// saving the string "I Saved it" to preferences with key = "didSave"
[defaults setObject:@"I Saved it" forKey:@"didSave"];
// now write it out to disk by synchronizing
[defaults synchronize];
// now read the key from the preferences
NSString* str = [defaults objectForKey:@"didSave"];
-----------------------
Note: there's another method, setStandardDefaults or something like
that, to which you can pass the default values. NSUserDefaults will
only save what differs from the standard defaults, and will
automatically return the defaults when no actual entry is present,
this saving you from having to add special code when the object for a
key is nil.
Your application's preferences will be saved in ~/Library/Preferences/ in a
file with your application's bundle identifier. For example
"com.apple.Safari" NSUserDefaults is a really nice API and is quite easy to
use.
The file name is then "com.apple.Safari.plist" -- user defaults are
stored in XML property-list format, and can be edited and viewed with
Apple's PListEditor. Also note that Cocoa's various AutoSave features
also write to this file, so don't be surprised to find a couple of
other keys there that you didn't write.
--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.