I am attempting to loop through all of a maps annotations and check to see if it’s a MKUserLocation annotation. If it’s not I want to remove it so I can add some other ones. I am trying to use the code below but it crashes w /the following error: “Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘* Collection <__NSArrayM: 0x1645d0> was mutated while being enumerated.”
for(id a in [[self mapview] annotations]) {
if([a class] != [MKUserLocation class]) {
[[self mapview] removeAnnotation:a];
}
}
How can I properly loop through all of the annotations and remove them while still ensuring that the userLocation marker isn’t removed.
Try operating on a copy of the annotations array, which should still work.
If annotations are added to the map view while this is running, they won’t be removed.
It looks like the map view is using a mutable array internally, and looping through it while removing objects would put it in an untenable state.