有没有更好的方法将此调用写入众多NSUserDefault BOOLS?

时间:2014-08-28 10:46:58

标签: ios boolean nsuserdefaults

我想要优化我的代码并改进我的编程。我正在看这个不断更新的声明。

是否有更好的方法可以调用多个BOOLS?是否可以将它们包含在FOR声明中?

baseballHat = [[NSUserDefaults standardUserDefaults] boolForKey:
 @"baseballHat"];

 asianHat = [[NSUserDefaults standardUserDefaults] boolForKey: @"asianHat"];

    cowboyHat = [[NSUserDefaults standardUserDefaults] boolForKey: @"cowboyHat"];

    topHat = [[NSUserDefaults standardUserDefaults] boolForKey: @"topHat"];

    partyHat = [[NSUserDefaults standardUserDefaults] boolForKey: @"partyHat"];

    sumbreroHat = [[NSUserDefaults standardUserDefaults] boolForKey: @"sumbreroHat"];

     wizardHat = [[NSUserDefaults standardUserDefaults] boolForKey: @"wizardHat"];

     beretHat = [[NSUserDefaults standardUserDefaults] boolForKey: @"beretHat"];

    pirateHat = [[NSUserDefaults standardUserDefaults] boolForKey: @"pirateHat"];

     vikingHelmet = [[NSUserDefaults standardUserDefaults] boolForKey: @"vikingHelmet"];


     if(baseballHat == YES){

         _baseballWearBut.visible = YES;
        _baseballHatOwned.visible = YES;

    }
      if(topHat == YES){

         //Continue with if statements

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

NSArray *array = @[@"baseball", @"asian", @"cowboy", @"top"]; //and the other ones
for (NSString *prefixKey in array) 
{
    NSString *key = [NSString stringWithFormat:@"%@Hat", prefixKey];
    BOOL boolValue = [[NSUserDefaults standardUserDefaults] boolForKey:key];
    [self setValue:@(boolValue) forKey:key]; 
}

我不推荐它,我不喜欢通过“他们的名字”调用变量(使用valueForKey:)。

另一种方法可能是使用NSDictionaryNSArray,但是,您需要将BOOL封装到对象(例如NSNumber)中,并可能增加另一个级别 您可能需要重新考虑您的架构应用程序 Hat只是一个BOOL吗?它有更多信息吗?它可能只是一个var到另一个对象(比如,创建一个带有var hat的类)? 你能为TRUE提供各种这样的帽子价值吗?如果没有,也许使用enumdef可能是一个很好的解决方案。

答案 1 :(得分:0)

// *** Following is few snippets ***

// *** Create a Dictionary with various states ***
NSMutableDictionary *dictStates = [[NSMutableDictionary alloc] init];
[dictStates setObject:[NSNumber numberWithBool:YES] forKey:@"asianHat"];
[dictStates setObject:[NSNumber numberWithBool:NO] forKey:@"cowboyHat"];
[dictStates setObject:[NSNumber numberWithBool:NO] forKey:@"topHat"];

// *** Save them to UserDefaults ***
[[NSUserDefaults standardUserDefaults] setObject:dictStates forKey:@"MyStates"];
[[NSUserDefaults standardUserDefaults] synchronize];

// *** Again Access all the various states stored in UserDefaults ***
NSMutableDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyStates"];

// *** Iterate through all the Objects ***
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSLog(@"%@ is %@",key , obj);
}];

// *** Modify Existing value and store again to UserDefaults ***
// *** Create Mutable Copy of Dictionary ***
NSMutableDictionary *dictModified = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MyStates"] mutableCopy];
// *** modify value ***
[dictModified setObject:[NSNumber numberWithBool:YES] forKey:@"topHat"];
// *** Save them to UserDefaults ***
[[NSUserDefaults standardUserDefaults] setObject:dictModified forKey:@"MyStates"];
[[NSUserDefaults standardUserDefaults] synchronize];