Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all you have a bug in your <code>if</code> conditional. You're checking the boolean value itself which, according to the <a href="http://developer.apple.com/Mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html#//apple_ref/occ/instm/NSUserDefaults/boolForKey:" rel="noreferrer"><code>boolForKey:</code> documentation</a>, will return a NO if it's not set yet. So <code>boolForKey:</code> is not the right way to do that.</p> <p>Here's two other ideas.</p> <p>Consider using another setting with another key to specify whether your settings have been initialized. Check it when you launch your app, or when you first read a setting. Initialize if needed. For instance:</p> <pre><code>- (void) initializeUserDefaults { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if (nil == [defaults objectForKey:@"initialized_defaults"]) { [defaults setBool:YES forKey:@"pref_sound"]; [defaults setBool:YES forKey:@"pref_vibrate"]; [defaults setObject:@"dummy_value" forKey:@"initialized_defaults"]; } } </code></pre> <p>A simpler solution (but I'm not sure if this would work) would be to change your conditional to read:</p> <pre><code>if (![prefs objectForKey:@"pref_sound"]) { </code></pre> <p>Again I don't know if this will do what I imagine it will, but I imagine that <code>objectForKey:</code> will return the underlying boxed boolean object, if it's there, or <code>nil</code>.</p> <p>If you add a new setting in a new version of your app, you don't want to leave your new settings uninitialized and you don't want to stomp your users' existing settings. This second method makes that effortless, and in the first method it's easier to screw up. But the first method also gathers all your settings in one place so you can see how it's supposed to work, which I like.</p> <p>I am not sure what ADC docs would consider a best practice though. To find that out, I'd suggest you look at any code samples referenced from the <code>NSUserDefaults</code> class reference.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload