Reputation: 99
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];
Upvotes: 0
Views: 709
Reputation: 9721
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.
Upvotes: 2