如何防止" [__ NSArrayM objectAtIndex:]:索引10超出界限[0 .. 9]'"

时间:2016-04-21 05:28:02

标签: ios nsarray

我有一个使用循环计数器的按钮操作方法,循环运行0到9然后触发另一个事件。我遇到的问题是,如果按下按钮的次数超过10次,则会抛出[__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 .. 9]' error。我知道这意味着按钮动作的启动次数比阵列配置的次数多,我想知道的是如何防止错误发生?因此,如果操作启动超过10次,则应用程序不会与[__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 .. 9]崩溃。任何帮助都会很棒。

这是循环计数器:

-(void) gameLoop {

if (loopCounter < 10){
    revealArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", nil];
    cover1.alpha = 1.0;
    cover2.alpha = 1.0;
    cover3.alpha = 1.0;
    cover4.alpha = 1.0;
    cover5.alpha = 1.0;
    cover6.alpha = 1.0;
    cover7.alpha = 1.0;
    cover8.alpha = 1.0;
    coreView.alpha = 1.0;

NSDictionary *question = nil;

question = [questionsArray objectAtIndex:loopCounter];

questionImage.image = [UIImage imageNamed:[question objectForKey:@"imageNames"]];
[self getAnswers];
}
else {
    //NSLog(@"finished");

   [self animateResults];
}

}

此代码创建数组:

-(void) getAnswers {

Tribes *tribes = [[Tribes alloc]init];
[tribes createArray];
answersArray = [[tribes optionsArray]mutableCopy];

NSDictionary *question = nil;
question = [questionsArray objectAtIndex:0];

 if ([[answersArray objectAtIndex:0] isEqualToString:[question objectForKey:@"answers"]] || [[answersArray objectAtIndex:1] isEqualToString:[question objectForKey:@"answers"]]){
   [self getAnswers];
}

else {
    [self answerLabelMethod];
    NSLog(@"%@", answersArray);

    questionTimer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(revealImage) userInfo:nil repeats:YES ];

}
}

我发现了这个方法的断点:

- (void) answerMethod:(int)answerBtn {

if (answerBtn == random) {
    //NSLog(@"correct!");
    int maxScore = 10000;
    int userScore = maxScore - (questionTimerCounter*1000);
    NSLog(@"%d", userScore);
    runningScore = runningScore + userScore;
    NSLog(@"%d" , runningScore);

    NSMutableDictionary *question = nil;
    //either at this point 
    question = [[questionsArray objectAtIndex:loopCounter]mutableCopy];
    //
    [question setObject:[NSNumber numberWithBool:YES] forKey:@"correct"];
    [question setObject:[NSNumber numberWithInt:userScore] forKey:@"score"];
    [questionsArray replaceObjectAtIndex:loopCounter withObject:question];
        }
else {

    NSMutableDictionary *question = nil;
    //or this point
    question = [[questionsArray objectAtIndex:loopCounter]mutableCopy];
    //
    [question setObject:[NSNumber numberWithBool:NO] forKey:@"correct"];
    [question setObject:[NSNumber numberWithInt:0] forKey:@"score"];
    [questionsArray replaceObjectAtIndex:loopCounter withObject:question];

}

GameInProgress = NO;

loopCounter++;
[self revealAnswer];

}

2 个答案:

答案 0 :(得分:1)

我想你的&#34;问题数组&#34;没有那么多容量来访问第11个元素,因为它只包含10个元素(0到9)

所以在这条线附近放置一个断点,你将获得一些解决方案 question = [questionsArray objectAtIndex:loopCounter];

答案 1 :(得分:0)

你正试图在索引中获取对象,我认为该数据超出了数组的范围。在需要时放置breakpoint并检查数组。添加exception breakpoint以检查您的应用崩溃的位置。