Re: User preferences

Richard Frith-Macdonald <[email protected]>
Newsgroups gmane.comp.lib.gnustep.user
Message-ID <[email protected]>
On 15 Dec 2004, at 23:50, Samuel Hornus wrote:

>
> Hi !
>
> Being happy with my little GUI, I would like to store/retreive some  
> user
> preferences. NSUserDefaults seems the way to go, and after reading some
> (but certainly not the most relevant :-( ) documentation, I followed  
> the
> advices from this web page:
>
> http://developer.apple.com/documentation/Cocoa/Conceptual/ 
> UserDefaults/Tasks/UsingDefaults.html#//apple_ref/doc/uid/20000719

Looks fine ... nothing there which should not work in GNUstep.

> Needless to say, it does not work. I kinda feel that I may need to
> supply some "default domain name" to the NSUserDefaults, but I can't  
> see
> where/what/when/why/...

No, you don't need to supply a domain name.

> Do you have some clues about saving some prefs and retrieving them ?
> What need be added to the code examplified in the webpage above ?
> Thank you in advance !

To save -

// Set a default string in the default domain for the application
[[NSUserDefaults standardUserDefaults] setObject: @"aValue" forKey:  
@"aKey"];

// Retrieve the value from the defaults system
value = [[NSUserDefaults standardUserDefaults] objectForKey: @"aKey"];

/*
  * NB. when a default value is set, it is cached in memory and not  
written to disk until
  * later on when the run loop is running and a regular housekeeping  
timer goes off.
  * If your program exits shortly after setting a default, or if your  
program is not using
  * a run loop, you need to explicitly flush the cached default data to  
disk by calling
  * the synchronize method.  The apple documentation does talk about  
this, but perhaps
  * not as clearly as it should.
  */
[[NSUserDefaults standardUserDefaults] synchronize];
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.