-[__NSArrayM objectAtIndex:]:索引 11 超出范围 [0 .. 10]' objective c

-[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]' objective c

大家好,关于 if(indexPath.row==3) 部分的问题是 -[__NSArrayM objectAtIndex:]:索引 11 超出范围 [0 .. 10]'。无法确定问题出在哪里。下面是我的代码

if(indexPath.row==3)
{
    cell.textLabel.text = @"Subject";
    cell.detailTextLabel.text=[empDict objectForKey:@"Subject"];
}
if(indexPath.row==3)
{
    cell.textLabel.text = @"Education";
    cell.detailTextLabel.text=[empDict objectForKey:@"Education"];
}


NSArray *times = [empDict objectForKey:@"ClassTimings"];

NSString *stgmt = [[times objectAtIndex:indexPath.row]objectForKey:@"GMT"];

NSString *strtime = [[times  objectAtIndex:indexPath.row]objectForKey:@"Time"];

NSString *strtimeZone = [[times  objectAtIndex:indexPath.row]objectForKey:@"TimeZone"];

NSString *str = [@[stgmt, strtime, strtimeZone] componentsJoinedByString:@" "];
if(indexPath.row==3)
{
    cell.textLabel.text = @"Times To Take";
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@",str];

}

return cell;

此问题是因为您试图访问包含 11 个元素的数组中的第 12 个元素。 在您的代码中,如果 times 数组没有 12 个元素并且您尝试访问第 12 个元素,则可能会发生此问题。也就是说,如果 indexpath.row = 12 并且您尝试通过 [times objectAtIndex:indexPath.row] 访问第 12 个对象会导致此错误。

你的问题不在"if(indexPath.row==3)"

您从以下三行中的任何一行收到错误

NSString *stgmt = [[times objectAtIndex:indexPath.row]objectForKey:@"GMT"];

NSString *strtime = [[times  objectAtIndex:indexPath.row]objectForKey:@"Time"];

NSString *strtimeZone = [[times  objectAtIndex:indexPath.row]objectForKey:@"TimeZone"];

您尝试访问 'times' 数组的 12th element(当值为 indexPath.row==12 时)的位置没有 12 elements...

希望对您有所帮助..