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?
First of all you have a bug in your
ifconditional. You’re checking the boolean value itself which, according to theboolForKey:documentation, will return a NO if it’s not set yet. SoboolForKey: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:
A simpler solution (but I’m not sure if this would work) would be to change your conditional to read:
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, ornil.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
NSUserDefaultsclass reference.