所以我是编程新手,并且已经坚持了好几天... 我有一个如下所示的数组
<?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 ...如何正确分配
答案 0 :(得分: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
的设置只会向您显示第一个问题,无论您拨打多少次。将此语句移动到构造函数或仅执行一次的位置。