如何防止“[__NSArrayM objectAtIndex:]:索引 10 超出范围 [0 .. 9]'”
How can I prevent "[__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 .. 9]'"
我有一个使用循环计数器的按钮操作方法,循环运行 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];
}
你试图在我认为超出数组范围的索引处获取对象。在需要时放置 breakpoint
并检查数组大小。添加 exception breakpoint
以检查您的应用崩溃的位置。
我猜你的 "question array" 没有那么大的容量来访问第 11 个元素,因为它只包含 10 个元素(0 到 9)
所以在这一行附近放置一个断点,你会得到一些解决问题的线索
问题 = [questionsArray objectAtIndex:loopCounter];
我有一个使用循环计数器的按钮操作方法,循环运行 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];
}
你试图在我认为超出数组范围的索引处获取对象。在需要时放置 breakpoint
并检查数组大小。添加 exception breakpoint
以检查您的应用崩溃的位置。
我猜你的 "question array" 没有那么大的容量来访问第 11 个元素,因为它只包含 10 个元素(0 到 9)
所以在这一行附近放置一个断点,你会得到一些解决问题的线索 问题 = [questionsArray objectAtIndex:loopCounter];