title: Removing object at index NSMutableArray
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8834031/objective-c-nsmutablearray-mutated-while-being-enumerated">Objective-C NSMutableArray mutated while being enumerated?</a>  </p>
  </blockquote>
  
  
  
  <p>I use this code to remove an object at index:</p>
  
  <pre><code>-(IBAction)deleteMessage:(id)sender{
  
  UIButton *button = (UIButton*) sender;
  
  for (UIImageView *imageView in imageArray) 
  {
  
      if ([imageView isKindOfClass:[UIImageView class]] &amp;&amp; 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]] &amp;&amp; text.tag == button.tag &amp;&amp; text.tag== name.tag)
                  {
  
          [imageView removeFromSuperview];
  
                      [messagename removeObjectAtIndex:button.tag - 1];
                      [messagetext removeObjectAtIndex:button.tag - 1];
  
                   }
           }
  
   }
  </code></pre>
  
  <p>The error is:</p>
  
  <pre><code>*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection &lt;__NSArrayM: 0x704bdb0&gt; was mutated while being enumerated.'
  </code></pre>
  
  <p>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)</p>