***由于未捕获的异常'NSRangeException'终止应用程序,原因:'***

时间:2010-11-27 02:57:00

标签: iphone

-(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;
}

*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [NSCFArray objectAtIndex:]:索引(-4(或可能更大))超出边界(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];
}

是肯定的。当我插入断点。错误信息在这里。

1 个答案:

答案 0 :(得分:1)

从其他代码段可以看出,错误位于[theQuiz objectAtIndex:row+x]行之一。

您的文件中的问题数量似乎与您的代码认为的最大问题数量不符。

要看的另一件事是r ow = ((QuestionNumber -1 *6)) ..你确定不会消极吗?