Settings Bundle toggle switch only returns YES

692 Views Asked by At

I have added a settings bundle to my application consisting of a number of toggle switches, It is being used to display different images depending on which toggle is active. This works fine the first time but once the values has been set to YES it always returns YES.

I have deleted the data within NSUserDefaults using the below

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

which works if the settings are changed before every launch of the application. (not ideal). Has anyone come across this and know how the values can be updated and persist? Below is my code where i am retrieving and seeing from BOOL value

BOOL setImageOne = (BOOL)[[NSUserDefaults standardUserDefaults]valueForKey:@"ladbrokes"];
BOOL setImageTwo = (BOOL)[[NSUserDefaults standardUserDefaults]valueForKey:@"williamHill"];

if (setImageOne) {
    self.clientLogoImageView.image = [UIImage imageNamed:@"clientOneLogo.png"];
}

if (setImageTwo) {
    self.clientLogoImageView.image = [UIImage imageNamed:@"clientTwoLogo.png"];
}

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
1

There are 1 best solutions below

0
On BEST ANSWER

The bool will be stored within an NSNumber object, as primitive types cannot be stored in Objective-C collection classes, so this statement:

BOOL setImageOne = (BOOL)[[NSUserDefaults standardUserDefaults]valueForKey:@"ladbrokes"];

is basically the same as:

NSNumber *boolObj = @(NO);
BOOL b = (BOOL)boolObj;      // b == YES

when what you intended was:

BOOL b = [boolObj boolValue];   // b == NO

Use [NSUserDefaults boolForKey] instead.