title: KVC Collection Accessor + index 0 beyond bounds for empty array error
body: <p>I have a mutable array property declared and synthesized:</p>
  
  <pre><code>@property (nonatomic, strong) NSMutableArray *arrayOfTasks;
  </code></pre>
  
  <p>I am using <a href="http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/KeyValueCoding/Articles/AccessorConventions.html#//apple_ref/doc/uid/20002174-BAJEAIEE" rel="nofollow">KVC collection Accessors</a> for the same property and also I have other methods which will internally call this KVC Collection accessor method like this:</p>
  
  <pre><code>-(void)insertObject:(CSTaskAbstract *)inTask inArrayOfTasksAtIndex:(NSUInteger)index
  {
          [[self arrayOfTasks] insertObject:inTask
                                    atIndex:index];
  }
  -(void)addObjectInArrayOfTasks:(CSTaskAbstract *)inTask
  {
          [self insertObject:inTask
       inArrayOfTasksAtIndex:[[self arrayOfTasks] count]];
  }
  </code></pre>
  
  <p>I had to do some modifications and add the object into the array only when a particular condition is satisfied, so to make sure that this check goes into the designated method, I included the following in the -insertObject KVC Collection accessor method:</p>
  
  <pre><code>-(void)insertObject:(CSTaskAbstract *)inTask inArrayOfTasksAtIndex:(NSUInteger)index
  {
      if ([inTask isOperatable])
      {
          [[self arrayOfTasks] insertObject:inTask
                                    atIndex:index];
      }
  }
  -(void)addObjectInArrayOfTasks:(CSTaskAbstract *)inTask
  {
          [self insertObject:inTask
       inArrayOfTasksAtIndex:[[self arrayOfTasks] count]];
  }
  </code></pre>
  
  <p>Now each time when I trigger <code>-addObjectINArrayOfTasks</code> method and if the <code>-isOperatable</code> condition returns boolean NO, the app crashes with no stack trace at all! (Stack trace is at main() of the application). All it says is <strong>"index 0 beyond bounds for empty array error"</strong>.</p>
  
  <p>I am not understanding the reason for this, I am not trying to access the array yet, so I am not giving a chance for framework to complain me that there is no element at index 0. Moreover, I am doing the count of array items check everywhere before accessing the objects out of array. For, if I was trying to access and element out of the bounds index, the app would crash at the same point and let me know exactly where I was trying to access the index out of bounds. That would have been a simple straightforward fix.</p>
  
  <p>Now, to just cross verify, I made a small change in the code like this, which seems to work:</p>
  
  <pre><code>-(void)insertObject:(CSTaskAbstract *)inTask inArrayOfTasksAtIndex:(NSUInteger)index
  {
          [[self arrayOfTasks] insertObject:inTask
                                    atIndex:index];
  }
  -(void)addObjectInArrayOfTasks:(CSTaskAbstract *)inTask
  {
      if ([inTask isOperatable])
      {
          [self insertObject:inTask
       inArrayOfTasksAtIndex:[[self arrayOfTasks] count]];
      }
  }
  </code></pre>
  
  <p>I can go ahead with this approach which is working and does not crash, but my concerns are the following:</p>
  
  <ol>
  <li><p>Adding the same check in designated method would be an added advantage in future if some other programmer would want to invoke the designated method from somewhere else.</p></li>
  <li><p>Why would the app crash in first case when I wont insert the object into the array in KVC collection accessors based on some condition check?</p></li>
  </ol>
  
  <p>Thanks for any inputs,</p>
  
  <p>Raj</p>