Reputation: 37
I've got a button action method that uses a loop counter, the loop runs 0 thru 9 then triggers another event. The problem I'm having is if the button is pressed more than 10 times it throws an [__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 .. 9]' error
. I know this means the button action has been initiated more times than the array is configured for, what I would like to know is how can I prevent the error from occurring? So that if the action is initiated more than 10 times the app doesn't crash with a [__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 .. 9]
. Any help would be great.
here is the Loop Counter:
-(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];
}
}
this code creates the array:
-(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 ];
}
}
Ive discovered the break point is in this method:
- (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];
}
Upvotes: 0
Views: 1658
Reputation: 424
I guess your "question array" does not have that much capacity to access 11th element as it holds only 10 elements (0 to 9)
So put a breakpoint near this line and you will get some lead to fix the problem question = [questionsArray objectAtIndex:loopCounter];
Upvotes: 1
Reputation: 27428
You are trying to get object at index which is out of bound of array i think. put breakpoint
whenever needed and check the arraysizes. add exception breakpoint
to check where your app is crashing.
Upvotes: 0