1

I am tying to optimise my code and improve my programming. I am looking at this statement which get updated constantly.

Is there a better way to right the calls to numerous BOOLS? Is it possible to include them in a FOR statement?

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
Ben Thompson
  • 263
  • 2
  • 5
  • 12

2 Answers2

1

This should do the trick:

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

I don't recommend it, I don't like calling variable by "their name" (using valueForKey:).

Another way could be to use a NSDictionary or an NSArray, but then, you'll need to encapsulate your BOOL into object (NSNumber for example), and may add another level.
You may have to reconsider your architecture app.
Is the Hat thing just a BOOL ? Does it has more info? Could it be just a var into a different object (like, creating a Class with a var hat)?
Can you have various of this hatvalues to TRUE? If not, maybe using a enumdef could be a good solution.

Larme
  • 24,190
  • 6
  • 51
  • 81
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];
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57