This code is causing the error, what could be wrong ?
I read something about locks, and that immutable objects can be susceptible to crashing when used on multiple threads. But really I do not know what that is all about…
-(void)cleanUpAllHexagons{
//Cleaning up all hexagons from previous level
NSLog(@"cleaning hexagons");
NSString *spriteKey;
NSString *touchAreaKey;
NSMutableDictionary *existingHexagons = [[GameStateSingleton sharedMySingleton]getExistingHexagons];
for (int i= 0; i < [existingHexagons count]; i++){
spriteKey = [NSString stringWithFormat:@"hexagon%d",i];
for (spriteKey in existingHexagons) {
NSLog(@"the spritekey = %@",spriteKey);
NSLog(@"%@", existingHexagons);
NSLog(@"%@", [[existingHexagons valueForKey:spriteKey]objectForKey:@"realSprite"]);
[self removeChild:[[existingHexagons valueForKey:spriteKey]objectForKey:@"realSprite"] cleanup:YES];
[existingHexagons removeObjectForKey:spriteKey];
}
hexTouchAreas = [[GameStateSingleton sharedMySingleton]getSharedHexTouchAreas];
touchAreaKey = [NSString stringWithFormat:@"hexTouchArea%d",i];
for (touchAreaKey in hexTouchAreas) {
NSLog(@"the touchAreakey = %@",touchAreaKey);
NSLog(@"%@", [hexTouchAreas valueForKey:touchAreaKey]);
[self removeChild: [hexTouchAreas valueForKey:touchAreaKey] cleanup:YES];
}
}
}
To enumerate all keys in this situation, you can use
So you can modify the dictionary while enumerating. However, If you are going to remove all keys anyway, shouldn’t you empty the dictionary after the loop using
removeAllObjectsmethod instead?