开发者

arc4random Quiz Crash

开发者 https://www.devze.com 2023-02-20 00:11 出处:网络
I\'m pretty new to all this. I\'ve read through the forums, but can\'t seem to find an answer. I have made a simple quiz with 20 questions (form a txt file)

I'm pretty new to all this. I've read through the forums, but can't seem to find an answer. I have made a simple quiz with 20 questions (form a txt file)

Trying to get arc4random() code to work to choose questions at random. It works, but crashes the app at different times. Can someone offer me some advice? thanks. Here is a some of my code:

Code:

// 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 scheduledTimer开发者_StackOverflowWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];

[selected release];
[activeQuestion release];

}


You probably should not release selected, as objectAtIndex returns autoreleased object.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号