0

我收到此错误:

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

这是应用程序崩溃的地方:

- (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;
}

我是 Objective C 的新手,我不知道为什么会这样。有人可以帮我吗?

4

3 回答 3

2

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;
}
于 2013-02-25T23:38:52.637 回答
0

大概就是这一行:

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

objectAtIndex返回一个对象,但您将结果分配给一个原语 ( NSUInteger,这是一个无符号整数的花哨名称)。我很惊讶 XCode 没有在这一行给你警告。你在存储annotations什么?

错误本身是一个越界错误 - 尽管我认为这意味着它可能是索引路径本身,正如 Jeremy 在评论中所建议的那样,您尝试访问数组的位置 556503008 的事实向我表明它不是。

于 2013-02-25T23:36:36.527 回答
0

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;
于 2013-02-26T00:22:00.997 回答