Iphone 未捕获异常:**-[\uu NSArrayM objectAtIndex:]:索引60超出范围[0..59]

Iphone 未捕获异常:**-[\uu NSArrayM objectAtIndex:]:索引60超出范围[0..59],iphone,arrays,exception,Iphone,Arrays,Exception,这里是我得到的值 m_数据源60和coord.color 2 如何解决这个问题?这是因为我假设您正在使用的NSArray从零开始吗?第60个对象位于索引59处?如果使用count方法并使用返回值获取索引处的对象,则必须减去1,因为2个对象的数组的计数为2,但对象1的索引为0,对象2的索引为1 int index=[someArraytoCount计数]-1 someObj*myObj=[otherArray objectAtIndex:index] 此外,使用逻辑可以确保安全 if(索引=0)

这里是我得到的值 m_数据源60和coord.color 2
如何解决这个问题?

这是因为我假设您正在使用的NSArray从零开始吗?第60个对象位于索引59处?

如果使用count方法并使用返回值获取索引处的对象,则必须减去1,因为2个对象的数组的计数为2,但对象1的索引为0,对象2的索引为1

int index=[someArraytoCount计数]-1

someObj*myObj=[otherArray objectAtIndex:index]

此外,使用逻辑可以确保安全

if(索引<[otherArray count]&&index>=0)


然后该索引将存在于数组中,并且不会出现应用程序崩溃

您做得不对:

    NSLog(@"m_datasource %d and coord.color %d",[m_dataSource.colors count],[coord.colorIndexes count]);

            cell.spotColor  = [m_dataSource.colors objectAtIndex: [coord.colorIndexes lastObject] intValue];
如果
coord.colorIndexes
中的最后一个对象是“100”,则检查statemnt-它将如下所示:

NSLog(@"m_datasource %d and coord.color %d",[m_dataSource.colors count],[coord.colorIndexes count]);
cell.spotColor  = [m_dataSource.colors objectAtIndex: [coord.colorIndexes lastObject] intValue];
但在
m_dataSource.colors
数组中只包含60个对象,这就是它将数组抛出索引异常的原因

将代码替换为以下代码段:

cell.spotColor  = [m_dataSource.colors objectAtIndex: 100]; 

希望这将帮助您简要介绍阵列概念

嗯,使用更大的数组或更小的索引。(请注意,一个常见错误是忘记数组索引运行0..N-1而不是1..N。)您今天遇到了相同的问题:。我不想听起来不友好,但您了解问题和解决方案吗?NSLog(@“m_datasource%d和coord.color%d”,[m_datasource.colors count],[coord.colordexes count]);cell.spotColor=[m_dataSource.colors objectAtIndex:([coord.colorIndexes lastObject]intValue]-1);试试看,它的末尾有一个'-1'。如果这不起作用,把你所有的代码发给我,我会把它分类
// get array count and substract with 1 - so it will retrun you last index of coord.colorIndexes array
cell.spotColor  = [m_dataSource.colors objectAtIndex: [coord.colorIndexes count]-1];