【问题标题】:iOS NSRangeException, Index Beyond Bounds, NSMutable ArrayiOS NSRangeException,索引超出范围,NSMutable 数组
【发布时间】:2011-09-30 18:17:41
【问题描述】:

在继承一个 iPhone 应用项目后,我的任务是在更新后对其进行调试。不是一个客观的 c 程序员,我不知道从哪里开始。任何帮助将不胜感激。

这是调试输出:

2011-07-07 15:27:44.333 App[5836:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x013d0be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x015255c2 objc_exception_throw + 47
    2   CoreFoundation                      0x013c66e5 -[__NSArrayM objectAtIndex:] + 261
    3   CollegeFootballAlerts               0x00034892 -[SMStandings tableView:cellForRowAtIndexPath:] + 397
    ...
)
terminate called after throwing an instance of 'NSException'

断点在teamInfo =:

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* cellIdentifier = @"TeamsInfoCell"; 

    SMStandingsTableViewCell * cell = (SMStandingsTableViewCell *)[tableView1 dequeueReusableCellWithIdentifier:cellIdentifier];
    if( cell == nil ){
        [[NSBundle mainBundle] loadNibNamed:@"SMStandingsTableViewCell" owner:self options:nil];
        cell = standingsTableCell;
        standingsTableCell = nil;
    }
    SMTeamsInfo * teamInfo;
        NSMutableArray * teamsInGroup = [allGroups objectForKey:[allKeys objectAtIndex:indexPath.section]];
        NSLog(@"TEAMS IN GROUP %@ :: %d", teamsInGroup, indexPath.row);
        teamInfo = [teamsInGroup objectAtIndex:indexPath.row];

    [[cell teamName] setText:[teamInfo nameEN]];
    [[cell teamFlag] setImage:[teamInfo teamFlag]];
    NSString * str;
    if([segmentedControl selectedSegmentIndex] == 0) {               // for conference tab wonconf - lostconf  combination is used 
        str= [NSString stringWithFormat:@"%@",[teamInfo wonconf]];
        str = [str stringByAppendingString:@"-"];
        str = [str stringByAppendingFormat:@"%@",[teamInfo lostconf]];
    }else {                                                          // for overall tab wonconf - lostconf  combination is used 
        str= [NSString stringWithFormat:@"%@",[teamInfo wonall]];
        str = [str stringByAppendingString:@"-"];
        str = [str stringByAppendingFormat:@"%@",[teamInfo lostall]];
    }

    [[cell currentStanding ]setText:str];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;    
}

还有numberOfRowsInSection方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray * array = [allGroups allKeys];
    NSLog(@"NO OF ROWS IN SECTION #%d : %d", section, [[allGroups objectForKey:[array objectAtIndex:section]] count]);  
    return [[allGroups objectForKey:[array objectAtIndex:section]] count];
}

以及该方法的输出:

2011-07-08 17:46:13.837 App[7513:207] NO OF ROWS IN SECTION #1 : 7
2011-07-08 17:46:13.837 App[7513:207] NO OF ROWS IN SECTION #0 : 6

不知道为什么第 1 节首先出现...行数是相反的。第 0 节应该是 7,第 1 节应该是 6,这似乎是问题的根源。

【问题讨论】:

    标签: iphone objective-c nsmutablearray terminate


    【解决方案1】:

    您在调试时是否在 xcode 中得到了这个?如果是这样,它应该自动将您带到它发生的线路。

    如果不是,则从SMStandingtableView: cellForRowAtIndexPath: 方法中某处的事物的外观来看,它正在发生。并且看起来像一个通用数组越界问题。如果您需要这方面的帮助,请使用该方法中的代码编辑您的问题。

    编辑:一般来说,对于这种堆栈跟踪,我会尝试找到一个类名/方法,它是我制作的,然后从那里开始。

    【讨论】:

    • 我确实从 xcode 控制台得到了它。我怎样才能让它带我去它发生的线路?
    • 它应该在代码窗口中自动执行。它实际上是半烦人的。如果没有,请尝试单击错误/警告列表中的错误(如果在左窗格中的 xcode 4 中第 4 个按钮三角形中的感叹号)
    • 在编辑中添加了断点...还没有更进一步。
    • 检查您的 numberOfRowsForSection: 方法以确保它是正确的。看起来您为特定部分的数组创建了太多行。很难说更多。
    • 有一个包含两个部分的表格视图,一个有 7 行,一个有 6 行。第一部分显示正确的数据,数组 (teamsInGroup) 显示正确数量的条目 (7)在调试输出中,但在进入第二部分之前只显示了 6 个。这里所有行都正确显示,但是当滚动到最后时,我认为它会查找第一节中缺少的行。
    【解决方案2】:

    这意味着您有一个包含 6 个项目的 NSArray,并且您要求索引 6 不存在,仅包含索引 0..5 存在。

    这发生在您的表格视图委托方法[SMStandings tableView:cellForRowAtIndexPath:] 内部,您可能在
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 中返回的数字太高

    【讨论】:

    • 是的,明白了错误的要点。但我似乎无法找到该数组被定义为分配附加索引的位置。
    • 它是从另一个对象中拉出来的,可能是这里的字典:NSMutableArray * teamsInGroup = [allGroups objectForKey:[allKeys objectAtIndex:indexPath.section]]; NSLog(@"TEAMS IN GROUP %@ :: %d", teamsInGroup, indexPath.row); teamInfo = [teamsInGroup objectAtIndex:indexPath.row];
    猜你喜欢
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多