I have an NSMutableArray
that stores mousejoints for a Box2d physics simulation. When using more than one finger to play I\'ll get exceptions stating
You can do something like this:
NSArray *tempArray = [yourArray copy];
for(id obj in tempArray) {
//It's safe to remove objects from yourArray here.
}
[tempArray release];
All the above did not work for me, but this did:
while ([myArray count] > 0)
{
[<your delete method call>:[myArray objectAtIndex:0]];
}
Note: this will delete it all. If you need to pick which items need to be deleted, then this will not work.
for(MyObject *obj in objQueue)
{
//[objQueue removeObject:someof];//NEVER removeObject in a for-in loop
[self dosomeopearitions];
}
//instead of remove outside of the loop
[objQueue removeAllObjects];
Maybe you deleted or added objects to your mutableAray while enumerating it. In my situation error appeared in this case.
I have come across this error and in my case it was because of the multi-threaded situation. One thread was enumerating an array while another thread removed objects from the same array at the same time. Hope this helps someone.
You can always iterate without an enumerator. Which means a regular for loop, and when you remove an object:- decrement the index variable and continue;. if you are caching the array's count before entering the for-loop, then make sure you decrement that too when removing an object.
Anyway, I do not see why an array with objects for later removal would be a problem. I do not know the exact situation which you are having and the technologies involved, but theoretically there should not be a problem. Because in most cases when using this method you can just do nothing in the first enumeration, and do the real work when enumerating the removal array. And if you have a situation where in the first enumeration you are checking something again against the same array and you need to know that the objects are not there anymore, you can just add a check to see if they are in the removal array.
Anyway, hope I helped. Good luck!