【问题标题】:index xxxxxxxxxx beyond bounds for empty array空数组的索引 xxxxxxxxxx 超出范围
【发布时间】:2018-01-02 21:12:10
【问题描述】:

我有一段代码崩溃了:

索引 18446744073709551615 超出空数组的范围

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

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

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

【问题讨论】:

  • 错误信息很清楚:arrayempty。 0 - 1 是 Int64.max (18446744073709551615)

标签: objective-c arrays


【解决方案1】:

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

显然,当至少有 2 个元素时,您只能访问倒数第二个元素,因此应将其包装在 if (mArrChatDetails.count > 1) { ... } 块中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多