尝试从iOS中的数组中删除对象时出现NSGenericException

时间:2012-10-04 10:28:09

标签: ios exception enumerator

我已经检查过如何解决这个问题的例子,但我仍然面对它..

我的代码是

NSEnumerator *enu= [obstacles objectEnumerator];
    NSMutableArray *delete = [[NSMutableArray alloc] init];

    Object *obj;
    while ((obj=[enu nextObject])!=nil)
    {

        if ([obj isKindOfClass: [BObject class]] &&  CGPointEqualToPoint(obj.position, point)    ) {
            [view.objects removeObject: obj];
            //[obstacles removeObject: obj]; 
            [delete addObject:obj];
            }
    }


    [obstacles removeObjectsInArray:delete];
    [delete release];

错误为*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x6a97ba0> was mutated while being enumerated.'

更新:如果我的obstacles数组中只有一个对象,则我的代码可以正常工作。它失败了多个对象..

0 个答案:

没有答案