2

Possible Duplicate:
Objective-C NSMutableArray mutated while being enumerated?

I use this code to remove an object at index:

-(IBAction)deleteMessage:(id)sender{

UIButton *button = (UIButton*) sender;

for (UIImageView *imageView in imageArray) 
{

    if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag == button.tag)
    {

        if (imageView.frame.size.height == 60) {
            x = 60;
        }

        if (imageView.frame.size.height == 200) {
            x = 200;
        }

        for (UITextView *text in messagetext) 
        {

            for (UITextView *name in messagename) 
            {

                if ([text isKindOfClass:[UITextView class]] && text.tag == button.tag && text.tag== name.tag)
                {

        [imageView removeFromSuperview];

                    [messagename removeObjectAtIndex:button.tag - 1];
                    [messagetext removeObjectAtIndex:button.tag - 1];

                 }
         }

 }

The error is:

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

I noticed though that if I delete first the last object in the array, and go in order from last to firs, it works. But if I try removing an object at an index that is not the last, the app crashes and gives the error: (1,2,3,4..I delete object2... crash...if I delete object 4 no crash)

0

3 Answers 3

8

One way to do this is to make an array with the indexes you intend to remove, the you do your loop, add the indexes and remove the objects afterwards. Something like this:

NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];

// Inside your loop
[indexes addIndex:(button.tag - 1)];

//..

// After your loop
[messagename removeObjectsAtIndexes:indexes];
[messagetext removeObjectsAtIndexes:indexes];

Now if you want different indexes for both arrays just make another NSMutableIndexSet and add the second set of indexes to it. Also don't forget to release indexes if you don't use ARC.

1
  • OK thanks this worked fine, by the way I us ARC
    – Alessandro
    Jul 12, 2012 at 18:11
1

You can't use "for each" style iteration on an array if you plan to mutate it (i.e., remove an element) because it messes with the iteration. If you really want to remove an element from the array as you iterate it, you need to use the "old style" iteration. Another Stack Overflow post here does a good job of showing how to use the old style that will allow you to mutate the array.

1

You can't use "for x in y" iteration on an array if you insert or remove objects from it. Either you have to use a good ol' fashioned array or you could keep a reference to the object you want to remove and then remove it afterwards:

NSObject *messageNameToRemove;
NSObject *messageTextToRemove; 
for (UITextView *text in messagetext) 
        {
        for (UITextView *name in messagename) 
        {

            if ([text isKindOfClass:[UITextView class]] && text.tag == button.tag && text.tag== name.tag)
            {

               [imageView removeFromSuperview];
               messageNameToRemove = [messagename objectAtIndex:button.tag -1];
               messageTextToRemove = [messagetext objectAtIndex:button.tag -1];
            }
         }

[messagename removeObject:messageNameToRemove];
[messagetext removeObject:messageTextToRemove];

Not the answer you're looking for? Browse other questions tagged or ask your own question.