索引xxxxxxxxxx超出空数组的边界

时间:2018-01-02 21:12:11

标签: objective-c arrays

我有一段崩溃的代码:

  

索引18446744073709551615超出空数组的界限

我知道应该首先检查数组是否为空,但我不确定如何。

NSIndexPath *indexPathSecondLast = [NSIndexPath indexPathForRow:[mArrChatDetails count]-1 inSection:0];
    ChatDetail *objChatOnCellSecondLast = [mArrChatDetails objectAtIndex:indexPathSecondLast.row]; 

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

[mArrChatDetails count]为0时会发生这种情况 - 即当您的表或集合为空时(我假设此上下文类似于UITableView或UICollectionView)。

显然你只能在至少有2个时访问倒数第二个元素,所以这应该包含在if (mArrChatDetails.count > 1) { ... }块中。