【问题标题】:*** Terminating app due to uncaught exception 'NSRangeException', reason: '****** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***
【发布时间】:2010-11-27 02:57:00
【问题描述】:
-(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:]: index (-4( or possible greater)) 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];
}

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

【问题讨论】:

  • 你确定loadquestion方法是错误的来源吗?
  • 我同意dreamlax,我不认为它发生在那里。
  • 是的。当我插入断点时。错误信息在这里。
  • 我已经插入了另一组代码。错误在那里?

标签: iphone


【解决方案1】:

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

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

要查看的另一件事是 row = ((QuestionNumber -1 *6)).. 你确定这不是负面的吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多