0

所以我是编程新手,并且已经坚持了好几天......我有一个数组,如图所示

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com    /DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
    <key>question</key>
    <string>2 + 2 = ?</string>
    <key>A</key>
    <string>2</string>
    <key>B</key>
    <string>7</string>
    <key>C</key>
    <string>3</string>
</dict>
<dict>
    <key>question</key>
    <string>2 + 3 = ?</string>
    <key>A</key>
    <string>5</string>
    <key>B</key>
    <string>2</string>
    <key>C</key>
    <string>6</string>
</dict>
</array>
</plist>

然后我想为每个存储一个数组。然后,我在故事板中为每个标签设置了一个标签,并希望将每个键分配给不同的标签,但我似乎做错了。这是我的代码

- (void)showNextQuestion
{
    NSArray *rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"question" ofType:@"plist"]];
    int numItems = [rootArray count];
    currentQuestion = -1;
    currentQuestion++;

    NSMutableArray *question = [NSMutableArray arrayWithCapacity:numItems];
    NSMutableArray *A = [NSMutableArray arrayWithCapacity:numItems];
    NSMutableArray *B = [NSMutableArray arrayWithCapacity:numItems];
    NSMutableArray *C = [NSMutableArray arrayWithCapacity:numItems];

    for (NSDictionary *itemData in rootArray) {
        [question addObject:[itemData objectForKey:@"question"]];
        [A addObject:[itemData objectForKey:@"A"]];
        [B addObject:[itemData objectForKey:@"B"]];
        [C addObject:[itemData objectForKey:@"C"]];

        self.questionasked.text =question;
        self.answer1.text = A;
        self.answer2.text = B;
        self.answer3.text = C;
    }
}

4个标签是问题,answer1,answer2,answer3......我如何正确分配它

4

1 回答 1

0

至少,您应该从 for 循环中删除分配并重写为:

    self.questionasked.text =question[currentQuestion];
    self.answer1.text = A[currentQuestion];
    self.answer2.text = B[currentQuestion];
    self.answer3.text = C[currentQuestion];

我还建议您只加载一次文件(而不是在每个 showNextQuestion 调用上)

此外,您的设置currentQuestion=-1只会向您显示第一个问题,无论您调用多少次。将此语句移动到构造函数或仅执行一次的地方。

于 2013-02-21T21:31:38.813 回答