获取内存地址而不是该地址处的对象

Getting a memory address instead of the object at that address

我正在编写一个程序,其中包含一个包含多个对象的数组。然后,我从该数组中取出特定对象并将它们的索引(来自 array1)作为 NSNumbers 存储在另一个数组中。现在,我试图通过在 table 视图中将索引从 array2 中拉出来在项目中稍后引用 array1 中的对象,但我不断收到一个错误,表明我正在获取对象的内存地址而不是对象本身。要遵循的代码:

self.indexArray = [NSMutableArray new];
    for (Item *item in array1){
        if (item.ID == comparedItem.ID){
            NSUInteger num = [array1 indexOfObject:item];
            NSNumber *numval = [NSNumber numberWithInteger:num];
            [self.indexArray addObject:numval];
        }
    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = (EditItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"quantityCell"];
int indexNum = [[self.indexArray objectAtIndex:(indexPath.row-1)] intValue];
NSString *descString = [NSString stringWithFormat:@"%@ %@ - %@",[[array1 objectAtIndex:indexNum]desc1],[[array1 objectAtIndex:indexNum]desc2],indexNum];
return cell;
}

所以我在行中收到一个错误:int indexNum = [[self.indexArray objectAtIndex:(indexPath.row-1)] intValue];,通常是这样的:*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 2]'。这是假设我在 运行 上面的代码之前向数组添加了 3 个对象(因此是 0 .. 2)。我假设 4294967295 是一个内存地址。有人知道我怎样才能得到对象本身吗?

谢谢!

4294967295-1 表示为无符号 int32。在此上下文中,它表示 indexPath.row-1 == -1indexPath.row == 0

我不确定你为什么要从你的 indexPath.row 中减去 1,但你应该添加一些安全措施来检查边界以确保你的数组查找不会获取一个超出范围的元素。

if (indexPath.row == 0) // do something special

[self.indexArray objectAtIndex:indexPath.row]