Question

I am getting this error:

-[__NSArrayM objectAtIndex:]: index 556503008 beyond bounds [0 .. 2]'

Here is where the app crashes:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger index = [[Data singleton].annotations objectAtIndex:indexPath.row];
    self.pinVC = [[PinViewController alloc]init];
    [self.pinVC setIdentifier:index];
    [[self navigationController]pushViewController:self.pinVC
                                      animated:YES];
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.pinArray.count;
}

I'm new to Objective C, and I don't know why this is happening. Can someone help me out?

Was it helpful?

Solution

It's likely that your pinArray variable isn't matching up to your call to objectAtIndex: on your singleton array. Assuming [[Data singleton].annotations holds the same type of information as your pinArray variable, then you might try:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSNumber *num = [self.pinArray objectAtIndex:indexPath.row];
    NSInteger index = [num integerValue]; //<-As an aside, observe I converted the number to integer type
    ...
    ...
}

The idea is that you are likely returning a higher row count than there are annotation objects in your array, hence the beyond bounds error.

Or else you should be doing this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSNumber *num = [[Data singleton].annotations objectAtIndex:indexPath.row];
    NSInteger index = [num integerValue]; //<-As an aside, observe I converted the number to integer type
    ...
    ...
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [Data singleton].annotations.count;
}

OTHER TIPS

It's probably this line:

    NSUInteger index = [[Data singleton].annotations objectAtIndex:indexPath.row];

objectAtIndex returns an object, but you're assigning the result to a primitive (NSUInteger, which is a fancy name for an unsigned int). I'm surprised XCode doesn't give you warning at this line. What are you storing in annotations?

The error itself is an out of bounds error - although I suppose that means it could be the index path itself, as Jeremy suggests in the comments, the fact you're trying to access position 556503008 of the array suggests to me that it's not.

So I realize that I meant to get the index of the object, but I was assigning the object to an NSUInteger, when it is a instance of a custom class. So I just changed that line of code to this:

NSUInteger index = indexPath.row;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top