views:

267

answers:

1

I have a setting in Root.plist with Key = 'latestNews' of type PSToggleSwitchSpecifier and DefaultValue as a boolean that is checked. If I understand that correctly, it should = YES when I pull it in to my code. I'm trying to check that value and set an int var to pass it to my php script. What is happening is that my boolean is either nil or NO and then my int var = 0. What am I doing wrong?


    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    int latestFlag = ([[prefs objectForKey:@"latestNews"] isEqualToString:@"On"]) ? 1 : 0;

    NSString *urlstr = [[NSString alloc] initWithFormat:@"http://www.mysite.com/folder/iphone-test.php?latest=%d", latestFlag];
    NSURL *url = [[NSURL alloc] initWithString:urlstr];

    //these are auto-released
    NSString *ans = [NSString stringWithContentsOfURL:url];
    NSArray *listItems = [ans componentsSeparatedByString:@","];

    self.listData = listItems;

    [urlstr release];
    [url release];

UPDATED: The real problem is that I am failing to retrieve any settings values. I changed my boolean setting to a string, similar to the books example. So now my setting has a TrueValue = "On" and a FalseValue = "Off" with a DefaultValue = "On". I am now trying to retrieve this setting with the code above. It should = "On" given that is the DefaultValue, however my int is ending up = 0, suggesting that I am failing to read the setting at all.

+2  A: 

When you set a boolean, design it so that never having set it is the same as it being false. If you want the default state of a checkbox to be checked, then set it to checked when the value is false, and uncheck it when the value is true.

In your case, change the name to "notLatestNews" and change the logic to match.

BOOL latestNews = ![[NSUserDefaults standardUserDefaults] boolForKey:@"notLatestNews"];
drawnonward
I think I understand what you are saying. However, in the plist file if you set the "DefaultValue" to be of type "Boolean", the value field changes to a checkbox. If you check that checkbox, then the default value for that setting should always be "YES" correct??I can change my logic, but I think my problem persists, that I am failing to read in the settings value properly.
dusk
There is nothing requiring that YES correlate to a checked checkbox. If you prefer not to invert the meaning of the preference, you can have a "defaultsVersion" preference and if it does not match your current version, set it along with any preferences that should have a default value. If you just want to distinguish nil from YES and NO, use objectForKey to check for nil before using boolValue instead of just boolForKey.
drawnonward
Just wanted to note that I'm taking a 3 day weekend. So I will get back to this Monday morning and let you know the results. Thanks again for your help. StackOverflow is awesome!
dusk
I updated my question above. To avoid any oddities with the boolean, I changed my switch value to a String, similar to how they did it in the book I'm using. However, in the book they are adding the setting values to UILabel's which get displayed on the view. I don't want to display the value, I just want to use it to control what gets passed to my PHP.
dusk