我创建了一个名为Questions的plist。当我创建plist时,它会自动创建一个名为“Root”的键。我如何将其重命名为“问题”?
这是我在.m文件中写的内容
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *path = [[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.questions = [tempDict objectForKey:@"Questions"]; //mine is called root not questions?
NSDictionary *firstQuestion = [self.questions objectAtIndex:0];
self.lblQuestionTitle.text = [firstQuestion objectForKey:@"QuestionTitle"];
self.lblAnswerA.text = [firstQuestion objectForKey:@"A"];
self.lblAnswerB.text = [firstQuestion objectForKey:@"B"];
if([firstQuestion objectForKey:@"C"]){
self.lblAnswerC.hidden = NO;
self.btnC.hidden = NO;
self.lblAnswerC.text =[firstQuestion objectForKey:@"C"];
}
else{
self.lblAnswerC.hidden = YES;
self.btnC.hidden = YES;
}
if([firstQuestion objectForKey:@"D"]){
self.lblAnswerD.hidden = NO;
self.btnD.hidden = NO;
self.lblAnswerD.text =[firstQuestion objectForKey:@"D"];
}
else{
self.lblAnswerD.hidden = YES;
self.btnD.hidden = YES;
}
}
这是我的plist - 我正在使用plist创建一个简单的测验 - 我在这里存储我的问题和答案:
<?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>QuestionTitle</key>
<string>What is sum of 1 + 1?</string>
<key>A</key>
<string>1</string>
<key>B</key>
<string>2</string>
<key>C</key>
<string>3</string>
<key>D</key>
<string>4</string>
<key>QuestionAnswer</key>
<string>B</string>
</dict>
<dict>
<key>QuestionTitle</key>
<string>When to wear ear defenders?</string>
<key>A</key>
<string>85db</string>
<key>B</key>
<string>88db</string>
<key>C</key>
<string>100db</string>
<key>D</key>
<string>70bd</string>
<key>QuestionAnswer</key>
<string>A</string>
</dict>
<dict>
<key>QuestionTitle</key>
<string>When a child cries in loud noise you should?</string>
<key>A</key>
<string>Ignore</string>
<key>B</key>
<string>Smile</string>
<key>C</key>
<string>Move Away from sound</string>
<key>D</key>
<string>Get annoyed</string>
<key>QuestionAnswer</key>
<string>C</string>
</dict>
<dict>
<key>QuestionTitle</key>
<string>1+3 =?</string>
<key>A</key>
<string>7</string>
<key>B</key>
<string>6</string>
<key>C</key>
<string>5</string>
<key>D</key>
<string>4</string>
<key>QuestionAnswer</key>
<string>D</string>
</dict>
<dict>
<key>QuestionTitle</key>
<string>5 + 5 =?</string>
<key>A</key>
<string>2</string>
<key>B</key>
<string>7</string>
<key>C</key>
<string>10</string>
<key>D</key>
<string>15</string>
<key>QuestionAnswer</key>
<string>C</string>
</dict>
答案 0 :(得分:4)
Root
只是Xcode中显示的标识符,表示plist中的基本容器。密钥Root
实际上从未添加到plist本身(除非您添加另一个Root
密钥)。 Root
通常是一个数组或字典,实际上并没有自己的密钥。
换句话说,你的plist中也不存在Questions
。 questions
将是将plist内容加载到...
你应该只有:
@property (strong, nonatomic) NSArray *questions;
self.questions = [[NSArray alloc] initWithContentsOfFile:path];