0
votes

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.

1
I'm still investigating, but I am starting to think the issue has something to do with connecting my app to the Settings.bundle. The AppSettings example app in the book starts out with the Utility Application template - and my app is just a standard View-based Application. So I'm trying to compare some of the other code for differences.dusk

1 Answers

2
votes

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