我正在关注Ray Wenderlich的一个教程,该教程涉及创建一个可以在Apple TV上显示问题的测验,以及要回答的设备。在这方面,很少有时间花在回答问题的逻辑上。问题存储在plist文件中,其中第一个可能的答案始终是正确的答案。在代码中,它显示为:
- (void)startQuestion
{
// 1
int questionIndex = arc4random_uniform((int)[self.questions count]);
NSMutableArray *questionArray = [self.questions[questionIndex] mutableCopy];
[self.questions removeObjectAtIndex:questionIndex];
// 2
NSString *question = questionArray[0];
[questionArray removeObjectAtIndex:0];
// 3
NSMutableArray *answers = [[NSMutableArray alloc] initWithCapacity:[questionArray count]];
self.currentQuestionAnswer = -1;
self.currentQuestionAnswersReceived = 0;
while ([questionArray count] > 0)
{
// 4
int answerIndex = arc4random_uniform((int)[questionArray count]);
if (answerIndex == 0 && self.currentQuestionAnswer == -1)
{
self.currentQuestionAnswer = [answers count];
}
[answers addObject:questionArray[answerIndex]];
[questionArray removeObjectAtIndex:answerIndex];
}
// 5
[self sendToAllPeers:[kCommandQuestion stringByAppendingString:
[NSString stringWithFormat:@"%lu", (unsigned long)[answers count]]]];
[self.scene startQuestionWithAnswerCount:[answers count]];
[self.mirroredScene startQuestion:question withAnswers:answers];
}
当在本教程结束时运行应用程序时,它总是使'A'成为正确的答案。有没有办法告诉应用程序plist中每个问题下的第一项是正确的,但让它在ABCD之间随机化,以便每次运行应用程序时答案都是不同的选择?