我对这一切都很陌生。我通过论坛阅读,但似乎无法找到答案。我做了一个简单的测验,有20个问题(形成一个txt文件)
尝试让arc4random()代码随意选择问题。它可以工作,但在不同的时间崩溃了应用程序。有人可以给我一些建议吗?谢谢。这是我的一些代码:
代码:
// Go to the next question
int i;
int j;
NSInteger row = 0;
i = 20; i > 1;
{
j = arc4random()%i;
questionNumber = j + 1;
if(questionNumber == 1)
{
row = questionNumber - 1;
}
else
{
row = ((questionNumber - 1) * 6);
}}
// Set the question string, and set the buttons the the answers
NSString *selected = [theQuiz objectAtIndex:row];
NSString *activeQuestion = [[NSString alloc] initWithFormat:@"%@", selected];
[answer1 setTitle:[theQuiz objectAtIndex:row+1] forState:UIControlStateNormal];
[answer2 setTitle:[theQuiz objectAtIndex:row+2] forState:UIControlStateNormal];
[answer3 setTitle:[theQuiz objectAtIndex:row+3] forState:UIControlStateNormal];
[answer4 setTitle:[theQuiz objectAtIndex:row+4] forState:UIControlStateNormal];
rightAnswer = [[theQuiz objectAtIndex:row+5] intValue];
// Set theQuestion label to the active question
theQuestion.text = activeQuestion;
// Start the timer for the countdown
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
[selected release];
[activeQuestion release];
}
答案 0 :(得分:1)
您可能不应该选择释放,因为objectAtIndex会返回自动释放的对象。