索引xxxxxxxxxx超出空数组的边界

问题描述 投票:0回答:1

我有一段代码崩溃:

索引18446744073709551615超出空数组的范围

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

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

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

objective-c arrays
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.