我在for循环中得到了一个BAD_ACCESS

时间:2012-11-03 02:45:29

标签: objective-c cocos2d-iphone

我有以下代码块

-(void) spawnEnemies:(ccTime)dt
{

    static float lastSpawn;
    lastSpawn += dt;

    if (lastSpawn > 0.75) {
        for (NSDictionary *enemyData in levelData.enemies) {
            if ([[enemyData valueForKey:@"spawnCount"] intValue] < [[enemyData valueForKey:@"quantity"] intValue]) {
                // reset spawn timer
                lastSpawn = 0;

                // init enemy
                id enemy = [NSClassFromString([enemyData valueForKey:@"className"]) enemyInit];
                [enemy startAnimating];
                [enemy setSpawnPosition];
                [enemy moveToSpriteInit:playerSprite];
                [self addChild:enemy];
                [enemy release];

                NSNumber *spawnCount = [enemyData valueForKey:@"spawnCount"];
                [enemyData setValue:[NSNumber numberWithInt:([spawnCount intValue] + 1)] forKey:@"spawnCount"];
            }
        }
    }
}

我想知道为什么这一行:

[enemyData setValue:[NSNumber numberWithInt:([spawnCount intValue] + 1)] forKey:@"spawnCount"];

抛出错误的访问错误?我试图在每次迭代时更新每个敌人的生成计数。我是否需要在for循环中重建levelData.enemies数组,并在for循环完成后将新更新的spawn计数值重新分配给levelData.enemies?

1 个答案:

答案 0 :(得分:0)

我很笨,我刚才意识到我没有将NS循环对象类型从NSDictionary更新为NSMutableDictionary,无法更改NSDict中的值,呃!