Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone dev - NSUserDefaults check for boolean existence

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?

like image 743
Matt Facer Avatar asked Dec 27 '09 11:12

Matt Facer


2 Answers

maybe the best idea is to register some defaults for the NSUserDefaults using

(void)registerDefaults:(NSDictionary *)dictionary

see Apple Documentation at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html#//apple_ref/doc/uid/20000318-SW5

To come back to your original problem: in the past I was using NSUserDefaults.dictionaryRepresentation in order to check the existence of a key:

BOOL playSound;
BOOL playVibrate;

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


if (playSound) {
    //do stuff
}

Hope this works for you.

like image 151
Bogdan Stanca-Kaposta Avatar answered Oct 25 '22 15:10

Bogdan Stanca-Kaposta


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.

like image 36
easeout Avatar answered Oct 25 '22 15:10

easeout