我有以下字典,它是从XML Parser中提取的。使用github库从以下链接解析XML文件。
XML to NSDictionary Parser Library
Quiz = {
questions = (
{
"@id" = 1;
answer = D;
A = "Under the rock";
B = "Trees";
C = "Mountain";
D = Water;
question = "Where does the fish live?";
},
{
"@id" = 2;
answer = D;
A = "Four";
B = "Two";
C = "Six";
D = Three;
question = "How many legs for a rabbit?";
},......
};
我正在尝试检索关键问题,答案,选项1-4的对象。但有些我怎么也不能传递价值。
for (NSString * key in xmlDictionary) {
NSDictionary * subDict = [xmlDictionary objectForKey:@"questions"];
NSLog(@"Correct Answers \n\n%@", [subDict objectForKey:@"answer"]);
// all your other code here.
}
不认为任何价值正在回归。实际上我在xml中有10个元素,在解析过程中它显示我有10个计数。但是,当我尝试计算xmlDictionary并返回1.不确定这里出了什么问题?
另外,我将如何提取所有值取决于@id?
困惑!!!
更新:我在XML文件中将...标签更改为...
NSDictionary *currentObject;
if (rowIndex < countXML)
{
currentObject=[muteArr objectAtIndex:rowIndex];
}
else
{
[self performSegueWithIdentifier:@"simple" sender:self];
}
NSString *questionLbl = [currentObject objectForKey:@"question"];
NSString *op1Lbl = [currentObject objectForKey:@"A"];
NSString *op2Lbl = [currentObject objectForKey:@"B"];
NSString *op3Lbl = [currentObject objectForKey:@"C"];
NSString *op4Lbl = [currentObject objectForKey:@"D"];
NSString *answer = [currentObject objectForKey:@"answer"];
NSString *questNoLbl = [currentObject objectForKey:@"@ID"];
questionID.text = [@"Question # " stringByAppendingString:questNoLbl];
question.text = questionLbl;
[answer1 setTitle:op1Lbl forState:UIControlStateNormal];
[answer2 setTitle:op2Lbl forState:UIControlStateNormal];
[answer3 setTitle:op3Lbl forState:UIControlStateNormal];
[answer4 setTitle:op4Lbl forState:UIControlStateNormal];
现在我有了这个代码,可以检索每个问题并在QuizViewController中显示它。当用户按下按钮时,它应该查看新的控制器并显示答案是否正确,并在单击ResultViewController中的“继续”按钮后返回到QuizViewController。现在我有了所有这些设置。
现在想在ResultViewController中显示答案是否正确。
答案 0 :(得分:1)
使用此行
NSDictionary * subDict = [xmlDictionary objectForKey:@"questions"];
你得到了相同的字典,你已经有了。
试试这个:
for (NSString * key in xmlDictionary) {
NSDictionary * subDict = [xmlDictionary objectForKey:key];
NSLog(@"Correct Answers \n\n%@", [subDict objectForKey:@"answer"]);
// all your other code here.
}
使用您编辑的XML结构,您应该这样做:
NSDictionary * rootDict = [xmlDictionary objectForKey:@"questions"];
for (NSString * key in rootDict) {
NSDictionary * subDict = [rootDict objectForKey:key];
NSLog(@"Correct Answers \n\n%@", [subDict objectForKey:@"answer"]);
// all your other code here.
}