• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSUserDefaultsController
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSUserDefaultsController


  • Subject: Re: NSUserDefaultsController
  • From: mmalcolm crawford <email@hidden>
  • Date: Sun, 9 Nov 2003 01:07:40 -0800

On Nov 8, 2003, at 8:03 PM, James Spencer wrote:

NSString * server = [[[NSUserDefaultsController sharedUserDefaultsController]
defaults] stringForKey:@"smtpServer"];

Thank you very much for this as it allows me to ask a more intelligent question, or at least I hope its more intelligent. This is what I initially tried. Unfortunately, what is being returned in server is nil.

It turns out things are a little less straightforward (and thanks to Scott for helping to clear this up).

If you have *saved* *new* defaults, then you can retrieve then from standard user defaults.

*Initial settings*, though, are *not* saved into the registration domain.

Given:

[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:
[NSDictionary dictionaryWithObject:@"smtp.mac.com" forKey:@"smtpServer"]];

and no other changes to user defaults:

NSLog(@"NSUserDefaultsController: %@", [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"smtpServer"]);
NSLog(@"NSUserDefaults: %@", [[NSUserDefaults standardUserDefaults] stringForKey:@"smtpServer"]);

returns

NSUserDefaultsController: smtp.mac.com
NSUserDefaults: (null)


If you now make a change and save, you get

NSUserDefaultsController: smtp.myisp.com
NSUserDefaults: smtp.myisp.com

If you revert to initial settings, you get back to

NSUserDefaultsController: smtp.mac.com
NSUserDefaults: (null)

See:
<http://homepage.mac.com/mmalc/CocoaExamples/SimplePreferences.zip>


So, if you are using the user defaults controller and you want to be able to retrieve initial settings (which is likely to be the case...), you should use

[[[NSUserDefaultsController sharedUserDefaultsController] values]


The documentation does not currently make this clear. Moreover it's not clear if this is really the behaviour that most would expect (personally, and speaking personally, I expected initial values to be stored automatically in the registration domain and accessible from standardUserDefaults...)? I'd be interested in others' views on this.

mmalc
_______________________________________________
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.

  • Follow-Ups:
    • Re: NSUserDefaultsController
      • From: Scott Anguish <email@hidden>
References: 
 >NSUserDefaultsController (From: James Spencer <email@hidden>)
 >Re: NSUserDefaultsController (From: mmalcolm crawford <email@hidden>)
 >Re: NSUserDefaultsController (From: James Spencer <email@hidden>)

  • Prev by Date: Re: AltiVec programming? Mailing list?
  • Next by Date: Re: Java and NSController
  • Previous by thread: Re: NSUserDefaultsController
  • Next by thread: Re: NSUserDefaultsController
  • Index(es):
    • Date
    • Thread