Removing Objects From NSMutableArray

Posted by Garry on Stack Overflow See other posts from Stack Overflow or by Garry
Published on 2010-05-30T16:04:35Z Indexed on 2010/05/30 16:12 UTC
Read the original article Hit count: 215

Hi,

I have a NSMutableArray that contains all the calendars on my system (as CalCalendar objects):

NSMutableArray *calendars = [[CalCalendarStore defaultCalendarStore] calendars];

I want to remove from calendars any CalCalendar objects whose title does not include the string @"work".

I've tried this:

for (CalCalendar *cal in calendars) {
    // Look to see if this calendar's title contains "work". If not - remove it
    if ([[cal title] rangeOfString:@"work"].location == NSNotFound) {
        [calendars removeObject:cal];
    }
}

The console is complaining that:

*** Collection <NSCFArray: 0x11660ccb0> was mutated while being enumerated.

And things go bad. Obviously it would seem you can't do what I want to do this way so can anyone suggest the best way to go about it?

Thanks,

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa