It seems like i have a logical problem in this loop. I am trying to:
- Looping myKeys (from a dict) to find selected players
- Setting three BOOL parameters for the selected players (YES or NO) in an NSMutableArray (thePlayers)
- Updating a dict (selectedPlayerDict) with ‘thePlayers’ and ‘myKeys’
- Empty thePlayers and do a new loop and the same thing
The individual players, when i select each player separately has the following parameters:
selectedPlayerDict:{Hannah = (0,0,1);}
selectedPlayerDict: {
AAAXXX = (
1,
1,
1
);
}
When is select both, meaning the trigger on (if ([theObjects objectAtIndex:3] == [NSNumber numberWithBool:YES])), i get the following result with
selectedPlayerDict: {
AAAXXX = (
0,
0,
1,
1,
1,
1
);
Hannah = (
0,
0,
1,
1,
1,
1
);
}
For the result above i use:
//[thePlayers removeAllObjects]; //<<<<<<<<<<<<<<<<<<<<<<<<<<
When i change and remove the commentary “//”:
[thePlayers removeAllObjects]; //<<<<<<<<<<<<<<<<<<<<<<<<<<
I get the following result:
selectedPlayerDict: {
AAAXXX = (
);
Hannah = (
);
}
The code i use is:
for (NSString *myKeys in allTheKeys) {
theObjects = [playerDict valueForKey:myKeys];
if ([theObjects objectAtIndex:3] == [NSNumber numberWithBool:YES]) {
//[thePlayers addObject:myKeys];
NSLog(@"Spelare: %@ är vald", myKeys);
NSLog(@">>>><<<<");
// Check what difficulties level
if ([theObjects objectAtIndex:0] == [NSNumber numberWithBool:YES]) { //hard
NSLog(@"Player have diff HARD");
[thePlayers addObject:[NSNumber numberWithBool:YES]]; //@"YES"];
}
else {
NSLog(@"Player have NOT diff HARD");
[thePlayers addObject:[NSNumber numberWithBool:NO]]; //@"NO"];
}
if ([theObjects objectAtIndex:1] == [NSNumber numberWithBool:YES]) { //medium
NSLog(@"Player have diff MEDIUM");
[thePlayers addObject:[NSNumber numberWithBool:YES]]; //@"YES"];
}
else {
NSLog(@"Player have NOT diff MEDIUM");
[thePlayers addObject:[NSNumber numberWithBool:NO]]; //@"NO"];
}
if ([theObjects objectAtIndex:2] == [NSNumber numberWithBool:YES]) { //easy
NSLog(@"Player have diff EASY");
[thePlayers addObject:[NSNumber numberWithBool:YES]]; //@"YES"];
}
else {
NSLog(@"Player have NOT diff EASY");
[thePlayers addObject:[NSNumber numberWithBool:NO]]; //@"NO"];
}
[selectedPlayerDict setValue:thePlayers forKey:myKeys];
[thePlayers removeAllObjects]; //<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}
NSLog(@"selectedPlayerDict: %@", selectedPlayerDict);
I know i probably do something wrong here with the logic but i have tried now for hours to try to understand what i am doing wrong. I do not understand why the dic’s objects is empty as i am “removeAllObjects” in the array in the end of the loop after i have updated the dict? I have tried “setObject:forKey:” but with no luck.
The problem is that you are reusing the same array for every key in selectedPlayerDict. You would need to create a fresh array for each loop iteration; otherwise, the
removeAllObjectsis removing all objects from the single array that is the value for every key in your dictionary.As a side note, I wonder if an
enum {Easy, Medium, Hard};might serve your need better than 3BOOLs — what will happen if the user selects > 1 of the options?