gpt4 book ai didi

iphone - 错误 '*** Collection <__NSCFDictionary: 0x563560> was mutated while being enumerated.'

转载 作者:行者123 更新时间:2023-12-01 17:45:03 25 4
gpt4 key购买 nike

此代码导致错误,可能是什么问题?
我读过一些关于锁的东西,不可变对象(immutable对象)在多个线程上使用时很容易崩溃。但我真的不知道那是怎么回事......

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

}


}

最佳答案

要枚举这种情况下的所有键,您可以使用

for (spriteKey in [existingHexagons allKeys])

因此,您可以在枚举时修改字典。但是,如果您要删除所有键,您不应该在循环后使用 removeAllObjects 清空字典吗?代替方法?

关于iphone - 错误 '*** Collection <__NSCFDictionary: 0x563560> was mutated while being enumerated.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8640565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com