I have an MutableArray filled with values from a database:
NSMutableArray *objectsArray = [[NSMutableArray alloc] init];
for (int n=0; n<[self.array count]; n++) {
[objectsArray addObject:[[self.array objectAtIndex:n] objectForKey:@"Name"]];
[objectsArray addObject:[[self.array objectAtIndex:n] objectForKey:@"Name_En"]];
}
I show the values on labels like so:
cell.textLabel.text = [[[dic objectForKey:@"d"] objectAtIndex:indexPath.row] objectForKey:@"Name"];
cell.detailTextLabel.text = [[[dic objectForKey:@"d"] objectAtIndex:indexPath.row] objectForKey:@"Name_En"];
So far the app works fine. It starts without throwing an exception and the values are in the labels. But if I start to scroll the app crashes and I get the following error:
Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (5) beyond bounds (5)'
Why is this error occurring?
You are probably saying the table that you have 6 rows, but you actually have 5. The reason for this exception is that the table view asks for cell for
indexPath.rowwith value of 5, which means it thinks that there are 6 [0:5] rows, but your array in the dictionary has 5 items [0:5).