我有一个属性列表文件,我用它来存储问题和答案数据。在这个文件中我有一个数组集合,在这些数组中我存储了不同的类别和问题。但是我似乎无法访问这些数据?
属性文件;
<?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">
<dict>
<key>Category1</key>
<array>
<array>
<string>Question1</string>
<string>Answer1</string>
</array>
<array>
<string>Question2</string>
<string>Answer2</string>
</array>
<array>
<string>Question3</string>
<string>Answer3</string>
</array>
<array>
<string>Question4</string>
<string>Answer4</string>
</array>
</array>
</dict>
</plist>
下面我使用两个简单的按钮来逐步访问属性列表文件;
-(IBAction)nextleft {
[self bingsound];
if (questionCounter > 1) {
questionCounter -= 1;
}
[self nextQuestion];
}
-(IBAction)nextright {
[self bingsound];
if (questionCounter < 20) {
questionCounter += 1;
}
[self nextQuestion];
}
-(void)nextQuestion {
NSArray *pair;
pair = [categories objectAtIndex:questionCounter];
plistQuestion = [pair objectAtIndex:0];
plistAnswer = [pair objectAtIndex:1];
abbreviation.text = plistQuestion;
}
我正在填充类别数组,如下所示;
categories = [NSArray arrayWithContentsOfFile:@"questions.plist"];
答案 0 :(得分:1)
您应该向arrayWithContentsOfFile:
提供plist文件的完整路径。以下应该有效:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"];
categories = [NSArray arrayWithContentsOfFile:filePath];
答案 1 :(得分:1)
您的属性列表包含字典作为根对象,而不是数组。
从顶部删除这些行:
<dict>
<key>Category1</key>
这一行从最后开始:
</dict>
另外,请关注@ murat的建议并获取文件的正确路径,而不是普通的字符串。
一般来说,这样的问题,调试器就是你的朋友。在您的方法中放置一个断点并检查每个阶段(派生路径,提取数组)您要获得的值。