Reputation: 12820
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];
}
}
}
Upvotes: 1
Views: 1164
Reputation: 9698
To enumerate all keys in this situation, you can use
for (spriteKey in [existingHexagons allKeys])
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 removeAllObjects
method instead?
Upvotes: 4
Reputation: 8664
You can't modify an object that you are using a fast enumeration on it.
So in your code you can't do
[existingHexagons removeObjectForKey:spriteKey];
inside of
for (spriteKey in existingHexagons)
Because you are modifying existingHexagons
You will need to use a regular for
loop.
Upvotes: 2