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,
While you can not remove items in an array that you are using fast enumeration on, you have some options:
-filterUsingPredicate:-indexesOfObjectsPassingTest:-removeObjectsInArray:As markhunte noted,
-calendarsdoesn’t neccessarily return a mutable array – you’d have to use-mutableCopyto get a mutable array which you can filter:… or e.g.
-filteredArrayUsingPredicate:for a immutable filtered copy.