0

我有以下从 XML Parser 中提取的字典。使用 github 库从以下链接解析 XML 文件。

XML 到 NSDictionary 解析器库

 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 中。当用户按下按钮时,它应该 Segue 到一个新的 Controller 并显示答案是否正确,并在单击 ResultViewController 中的“继续”按钮后返回 QuizViewController。现在我已经完成了所有这些设置。

现在想在 ResultViewController 中显示答案是否正确。

4

1 回答 1

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.
    }
于 2013-03-21T07:42:33.817 回答