0

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

1 回答 1

2

bool 将存储在一个NSNumber对象中,因为原始类型不能存储在 Objective-C 集合类中,所以这个语句:

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

基本上是一样的:

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

当你的意图是:

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

改为使用[NSUserDefaults boolForKey]

于 2015-07-08T15:13:25.597 回答