*** Terminating app due to uncaught exception 'NSRangeException', reason: '***
-(void)loadquestion {
NSBundle *bundle = [NSBundle mainBundle];
NSString *textfilePath = [bundle pathForResource:@"MathPSLE" ofType:@"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:textfilePath encoding:NSUTF8StringEncoding error:nil];
NSArray *quizArray = [[NSArray alloc] initWithArray:[fileContents componentsSeparatedByString:@"\n"]];
theQuiz = quizArray;
}
* Terminating app due 开发者_开发知识库to uncaught exception 'NSRangeException', reason: '* -[NSCFArray objectAtIndex:]: index (-4( or possibly larger)) beyond bounds (300)'
-(void)askquestion {
QuestionNumber = QuestionNumber +1;
NSInteger row = 0;
if (QuestionNumber == 1) {
row = QuestionNumber -1;
}
else {
row = ((QuestionNumber -1 *6));
}
NSString *selected = [theQuiz objectAtIndex:row];
NSString *activeQuestion = [[NSString alloc] initWithFormat:@"Question: %@",selected];
[ansButton1 setTitle:[theQuiz objectAtIndex:row+1] forState: UIControlStateNormal];
[ansButton2 setTitle:[theQuiz objectAtIndex:row+2] forState: UIControlStateNormal];
[ansButton3 setTitle:[theQuiz objectAtIndex:row+3] forState: UIControlStateNormal];
[ansButton4 setTitle:[theQuiz objectAtIndex:row+4] forState: UIControlStateNormal];
rightAnswer = [[theQuiz objectAtIndex:row+5]intValue];
questionLabel.text = activeQuestion;
[selected release];
[activeQuestion release];
}
-(IBAction)next_Button {
[self askquestion];
}
yes. when i insert the breakpoint. the error message is here.
It's pretty obvious from your other code segment the error is somewhere in one of the [theQuiz objectAtIndex:row+x]
lines.
It seems like the number of questions you have in your file may not match the maximum number of questions your code thinks it has.
Another thing to look at would be row = ((QuestionNumber -1 *6))
.. are you sure that's not winding up negative?
精彩评论