我必须从数组中删除实例,但是当我删除实例时,将删除具有相同值的所有实例。我使用的代码适用于其他对象,如Sprites
,但不适用于整数。
谁能指出我正确的方向?
deleteSpriteNr
的值是de object to delete,我在for循环结束时也使用了[levelSpriteNrArray removeObjectAtIndex:deleteSpriteNr],但这会导致崩溃。
代码:
int tel3 = 0;
for (NSNumber *spriteNr in levelSpriteNrArray ){
if ( tel3 == deleteSpriteNr ){
[spriteNrToRemove addObject:spriteNr];
//[spriteNr removeFromParentAndCleanup:YES] // does not works with integer;
}
tel3++;
}
[levelSpriteNrArray removeObjectsInArray:spriteNrToRemove];
答案 0 :(得分:0)
尝试这样做。希望这会有所帮助。
int tel3 = 0;
NSMutableIndexSet *indexes = [NSMutableIndexSet new];
for (int i = 0; i < [levelSpriteNrArray count]; i++ ){
if ( tel3 == deleteSpriteNr ){
[indexes addIndex:i];
//[spriteNr removeFromParentAndCleanup:YES] // does not works with integer;
}
tel3++;
}
[levelSpriteNrArray removeObjectsAtIndexes:indexes];