开发者

looping array and saving objects in NSMutableDictionary does not work?? logical problem?

开发者 https://www.devze.com 2023-01-27 03:19 出处:网络
It seems like i have a logical problem in this loop. I am trying to: Looping myKeys (from a dict) to find selected players

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:myK开发者_如何转开发eys];

    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 removeAllObjects is 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 3 BOOLs -- what will happen if the user selects > 1 of the options?


Other answers have already found the source of your problem, but here is a potential other problem:

[theObjects objectAtIndex:2] == [NSNumber numberWithBool:YES]

This appears to work because [NSNumber numberWithBool:YES] is probably cached somewhere, however keep in mind that == compares object identity, not object equality. If you know that the objects in the theObjects array are NSNumber objects, you could use boolValue:

if ([[theObjects objectAtIndex:2] boolValue])
   ...

If you are unsure whether the objects in the array are NSNumber instances, you can use the following:

if ([[theObjects objectAtIndex:2] isEqual:[NSNumber numberWithBool:YES]])
   ...


You are clearing the array that you just added to the dictionary.

Try this instead:

NSArray* tempArray = [thePlayers copy];
[selectedPlayerDict setValue:tempArray forKey:myKeys];   
[tempArray release];
0

精彩评论

暂无评论...
验证码 换一张
取 消