0
在我的測驗中,我有幾個答案。正確答案在plist中。 這裏是如何檢查值?
<array>
<dict>
<key>QuestionTitle</key>
<string>Веки являются</string>
<key>Answers</key>
<array>
<string>частью глазного яблока</string>
<string>защитным аппаратом органа зрения</string>
<string>и тем, и другим</string>
<string>ни тем, ни другим</string>
</array>
<key>CorrectAnswer</key>
<array>
<integer>0</integer>
<integer>1</integer>
</array>
</dict>
現在我無法猜測如何讓檢查的答案是用戶回答 我可能會爲A,B,C,d的答案變數0,1,2,3但如何我應該比較它與CorrectAnswer陣列 有我的米。文件代碼
NSString* path = [[NSBundle mainBundle] pathForResource:@"Questions2" ofType:@"plist"];
NSMutableArray *questionsDict = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSUInteger count = [questionsDict count];
for (NSUInteger i = 0; i < count; ++i)
{
int nElements = count - i;
int n = (arc4random()% nElements) + i;
[questionsDict exchangeObjectAtIndex:i withObjectAtIndex:n];
}
self.questions = [questionsDict copy];
currentQuestion = 0;
NSDictionary* nextQuestion =
[self.questions objectAtIndex: currentQuestion];//[self.questions objectForKey:
[NSString stringWithFormat:@"%d", currentQuestion]];
NSMutableArray *array = [nextQuestion[@"Answers"] mutableCopy];
self.labelA.text = [array objectAtIndex:0];
self.labelB.text = [array objectAtIndex:1];
self.labelC.text = [array objectAtIndex:2];
self.labelD.text = [array objectAtIndex:3];
self.labelQuestion.text = [nextQuestion objectForKey:@"QuestionTitle"];
//Button D (A,B,C the same)
- (IBAction)fourButton:(id)sender
{
if (chekColorD == 0)
{
chekColorD++;
[buttonD setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
//self.stringD = @"D";
}
else
{
chekColorD = 0;
[buttonD setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// self.stringD = nil;
}
}
//NExt question button
- (IBAction)nextQuestion:(id)sender
{
// Тут делаешь сравнение правильных ответов
chekColorA = 0; chekColorB = 0; chekColorC = 0; chekColorD = 0;
[buttonA setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[buttonB setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[buttonC setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[buttonD setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.labelScore.text = [NSString stringWithFormat:@"%d", numCorrect];
if ([self.highScore integerForKey:@"HighScore"]<numCorrect){
[self.highScore setInteger:numCorrect forKey:@"HighScore"];
[self.highScore synchronize];
}
currentQuestion++;
if (currentQuestion <= [self.questions count]){
self.labelScore.text = [NSString stringWithFormat:@"%d", numCorrect];
self.labelHighestScore.text = [NSString stringWithFormat:@"%d",
[self.highScore integerForKey:@"HighScore"]];
NSDictionary* nextQuestion = [self.questions objectAtIndex: currentQuestion];
NSMutableArray *array = [nextQuestion[@"Answers"] mutableCopy];
self.labelA.text = [array objectAtIndex:0];
self.labelB.text = [array objectAtIndex:1];
self.labelC.text = [array objectAtIndex:2];
self.labelD.text = [array objectAtIndex:3];
self.labelQuestion.text = [nextQuestion objectForKey:@"QuestionTitle"];
//NSLog(@"%d количество вопросов", countQuestion);
}
else{
currentQuestion --;
}
}
OK,但如果用戶接聽第一個「B」(1 )然後「A」(0)字符串將是1,0但CorrectAnswer數組有整數0,1 –
比較例如[labelA.text isEqualToString:correctAnswerString] – incmiko
我編輯我的答案使其可以理解 – incmiko