Mohammed Ebrahim
Mohammed Ebrahim

Reputation: 869

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x138cb4d0> was mutated while being enumerated

i am trying to remove duplicate objects from array.

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

i am adding two object which is already their in the array

Upvotes: 2

Views: 6905

Answers (5)

Nikita P
Nikita P

Reputation: 4246

you cannot modify an array when you are enumerating it. you can do the following:

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

Upvotes: 6

Edwin Vermeer
Edwin Vermeer

Reputation: 13127

you can not use a for loop and then add or remove objects from the array that you are iterating through (arrSelectedVehicle). Instead try building up a new array with the objects that are OK. At the end of the loop you could assign that array back to arrSelectedVehicle.

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 385580

There are several problems with your code. Anyway, the easiest way to remove duplicates, if you don't care about the order of the elements, is using an NSSet, because an NSSet doesn't allow duplicates:

NSArray *uniqueObjects = [[NSSet setWithArray:arrSelectedVehicle] allObjects];

Upvotes: 1

rmaddy
rmaddy

Reputation: 318794

You can't modify an array while using it with fast enumeration. That's what the error is telling you. You need to change the loops

for (NSUInteger i = 0; i < arrSelectedVehicle.count; i++) {
    Vehicles *a1 = arrSelectedVehicle[i];
    copy = YES;
    for (NSUInteger j = 0; j < filterResults.count; j++) {
        Vehicles *a2 = filterResults[j];

        if ([a1.Vehicle_id isEqualToString:a2.Vehicle_id]) {
            copy = NO;
            [arrSelectedVehicle removeObjectIdenticalTo:a2];
            break;
        }
    }
    if (copy) {
        [filterResults addObject:a1];
    }
}

Upvotes: 1

user529758
user529758

Reputation:

The error message says it pretty much: you can't modify the contents of a mutable collection while you're using fast enumeration on it (because that's erroneous). You have to make a mutable copy of it, and remove the duplicates from that copy.

Upvotes: 0

Related Questions