我试图从数组中删除重复的对象。

 NSMutableArray* filterResults = [[NSMutableArray alloc] init];
    BOOL copy;

    // remove duplicate
    if (![arrSelectedVehicle count] == 0)
    {
        for (Vehicles *a1 in arrSelectedVehicle) {
            copy = YES;
            for (Vehicles *a2 in filterResults) {
                if ([a1.Vehicle_id isEqualToString:a2.Vehicle_id]) {
                    copy = NO;
                    [arrSelectedVehicle removeObjectIdenticalTo:a2];
                    break;
                }
            }
            if (copy) {
                [filterResults addObject:a1];
            }
        }
    }

我要添加两个已经在数组中的对象

最佳答案

枚举时无法修改数组。您可以执行以下操作:

 NSMutableArray* filterResults = [[NSMutableArray alloc] init];
BOOL copy;

// remove duplicate
if (![arrSelectedVehicle count] == 0)
{
NSArray* arraycopy = [arrSelectedVehicle copy];
    for (Vehicles *a1 in arraycopy) {
        copy = YES;
        for (Vehicles *a2 in filterResults) {
            if ([a1.Vehicle_id isEqualToString:a2.Vehicle_id]) {
                copy = NO;
                [arrSelectedVehicle removeObjectIdenticalTo:a2];
                break;
            }
        }
        if (copy) {
            [filterResults addObject:a1];
        }
    }
[arraycopy release];
}

关于objective-c - 由于未捕获的异常'NSGenericException'而终止应用程序,原因:'***集合<__ NSArrayM:0x138cb4d0>在枚举时发生了突变,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14152471/

10-10 20:49