Search code examples
iphoneobjective-ccocoa-touchcrashnsrange

Getting NSRangeException Beyond Bounds Crash


I have this code that is crashing with error *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 0 beyond bounds for empty array'

at this line:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyObject *myCode = [self.fetchedResultsController objectAtIndexPath:indexPath];
}

I don't see whats wrong here, it is pulling the data from a fetch.


Solution

  • The answer is right in your post 'reason: ' ... index 0 beyond bounds for empty array'. Your self.fetchedResultsController is empty now you need to investigate why.

    Note: Whenever you modify the underlying data for a table view you need to update the changes by either calling -[UITableView reloadData]; or the beginUpdates and endUpdates methods then adding or removing the correct index paths.