views:

823

answers:

1

Hi... I've just added a settings bundle to my app and am having trouble reading the bool settings. I know that upon launch of the app, the settings are not read unless the user actually enters them - and that's what I am trying to capture.

However, my code is simply capturing if the answer is NO OR they havent been set. I need to find out if they've been set, THEN set answers!

setting code:

BOOL playSound;
BOOL playVibrate;


//test for some defaults
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (![prefs boolForKey:@"pref_sound"]) {
 playSound = YES;
 playVibrate = YES; 
} else {
 playSound = [prefs boolForKey:@"pref_sound"];
 playVibrate = [prefs boolForKey:@"pref_vibrate"]; 
}


if (playSound) {
//do stuff
}

the problem is, if the user sets the settings to "NO", the code then changes both vibrate AND sound to yes - which is meant to be the capture for NOT setting....

any ideas?

+3  A: 

First of all you have a bug in your if conditional. You're checking the boolean value itself which, according to the boolForKey: documentation, will return a NO if it's not set yet. So boolForKey: is not the right way to do that.

Here's two other ideas.

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:

- (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"];
    }
}

A simpler solution (but I'm not sure if this would work) would be to change your conditional to read:

if (![prefs objectForKey:@"pref_sound"]) {

Again I don't know if this will do what I imagine it will, but I imagine that objectForKey: will return the underlying boxed boolean object, if it's there, or nil.

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.

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 NSUserDefaults class reference.

Kevin Conner
thanks for this - the objectForKey seems to have done the trick! I did notice the boolForKey would return NO if not set, so tried == nil, but that didnt work. thanks :)
Matt Facer
My pleasure :) Last tip, make use of option-double-click in Xcode. Apple's documentation is amazingly thorough so you can usually solve your problems via "what does this method do"? Cheers!
Kevin Conner