title: Xcode exception: "Collection <__NSArrayM: 0xb1a2970> was mutated while being enumerated" with Mutable Arrays
body: <p>I have a problem with three clases. The the first class is called <code>Player</code>. This class has an <code>NSMutableArray</code> inside it called <code>units</code>. This array is made up of objects of the class <code>Unit</code>. This class in turn, has an <code>NSMutableArray</code> called <code>bullets</code>. It works like this:</p>
  
  <p>At a certain point the class <code>Player</code> (it could just be the <code>ViewController</code> instead) adds an object to the <code>units</code>. Then, when an instance of <code>Unit</code> is initialized, as a result of the above, it creates an NSTimer that is in charge of creating bullets every second.</p>
  
  <p>The thing is, is crashes somewhere in the middle of this with a <code>SIGABRT</code> that tells me that there was an exception because: <code>Collection &lt;__NSArrayM: 0xb1a2970> was mutated while being enumerated.</code> Also, I took away the line that created bullets and it stops crashing, proving that is the problem. What does that mean!</p>
  
  <p>Here is a bit of executable code that might work:</p>
  
  <p>ViewController.h (instead of player)</p>
  
  <pre><code>@interface ViewController : UIViewController
  {
      NSMutableArray *units;
      NSTimer *updateTimer;
  }
  -(void)Update;
  </code></pre>
  
  <p>ViewController.m</p>
  
  <pre><code>@implementation ViewController
  //methods...
  
  - (void)viewDidLoad
  {
      //more default code
  
      //Initialized array and adds one object with the default constructor for simplicity
      units = [[NSMutableArray alloc] initWithObjects:[[Unit alloc] init], nil]
  }
  
  -(void)Update
  {
      for(Unit *unit in units)
      {
          [unit Update];
          if(unit.deleteFromList)
              [units removeObject:unit];   
      }
  }
  //More methods
  @end
  </code></pre>
  
  <p>Unit.h</p>
  
  <pre><code>@interface Unit : NSObject
  {
      NSMutableArray *bullets;
      NSTimer *bulletTimer;
      boolean deleteFromList;
  }
  
  @property(readonly, assign)deleteFromList;
  
  -(void)Fire;
  
  -(void)Update;
  </code></pre>
  
  <p>Unit.m</p>
  
  <pre><code>@implementation Unit
  
  @synthesize deleteFromList;
  
  -(id)init
  {
      if(self)
      {
          bullets = [[NSMutableArray alloc] init];
          bulletTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(fire) userInfo:NULL repeats:true];
          deleteFromList = false;
      }
  
      return self;
  }
  
  -(void)Fire
  {
      [bullets addObject:[[Bullet alloc] init]];
  }
  
  -(void)Update
  {
      for(Bullet *bullet in bullets)
      {
          [bullet Update];
          if(bullet.deleteFromList)
              [bullets removeObject:bullet];
      }
  
      if(certainCondition)
          deleteFromList = true;
  }
  </code></pre>
  
  <p>The bullet class will be omitted because the contents are irrelevant to what happens. Also, all the classes and constructors were shortened because the rest is useless for this example</p>
  
  <p>EDIT:</p>
  
  <p>Another thing I forgot to add is that the timer is created in an enumeration of the NSMutableArray units in the update method i'm about to add. I'm also adding a variable to Unit and Bullet that orders it to delete. The bullet update changes the position and also changes the deleteFromList variable</p>