.plist访问字典数组?

时间:2014-06-26 20:15:35

标签: ios objective-c

我的.plist以数组根开头,然后有多个词典键,每个词包含六个项目。

    - (void)viewDidLoad
    {


        NSString *path = [[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"];

        self.questions = [NSArray arrayWithContentsOfFile:path];

        NSDictionary *firstQuestion = [self.questions objectAtIndex:0];

        self.questionLabel.text = [firstQuestion objectForKey:@"QuestionTitle"];

        [self.ansOne setTitle:[firstQuestion objectForKey:@"A"] forState:UIControlStateNormal];
        [self.ansTwo setTitle:[firstQuestion objectForKey:@"B"] forState:UIControlStateNormal];
        [self.ansThree setTitle:[firstQuestion objectForKey:@"C"] forState:UIControlStateNormal];
        [self.ansFour setTitle:[firstQuestion objectForKey:@"D"] forState:UIControlStateNormal];

但我加载了空白标签,这是我的.plist的XML

        <?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>Which of the following probes will achieve the highest penetration?</string>
                <key>A</key>
                <string>5.0 Mhz</string>
                <key>B</key>
                <string>4.0 Mhz</string>
                <key>C</key>
                <string>3.4 Mhz</string>
                <key>D</key>
                <string>1.0 Mhz</string>
                <key>QuestionAnswer</key>
                <string>D</string>
            </dict>
            <dict>
                <key>QuestionTitle</key>
                <string></string>
                <key>A</key>
                <string></string>
                <key>B</key>
                <string></string>
                <key>C</key>
                <string></string>
                <key>D</key>
                <string></string>
                <key>QuestionAnswer</key>
                <string></string>
            </dict>
            <dict>
                <key>QuestionTitle</key>
                <string></string>
                <key>A</key>
                <string></string>
                <key>B</key>
                <string></string>
                <key>C</key>
                <string></string>
                <key>D</key>
                <string></string>
                <key>QuestionAnswer</key>
                <string></string>
            </dict>
        </array>
        </plist>

我正在关注一个教程并且很多其他人都面临这个问题,有人能指出我正确的方向,或者为我修复代码吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我使用NSPropertyListSerialization修复了这个问题:

NSString *error;
   NSPropertyListFormat format;
   id obj = [NSPropertyListSerialization propertyListFromData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"]] mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&error];
   if ([obj isKindOfClass:[NSDictionary class]]) {
          NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:obj];
      } else {
          NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:obj];
   }

答案 1 :(得分:1)

我的应用程序执行与您尝试执行的操作相同的操作,但我的plist root是字典而不是数组。

而不是:

self.questions = [NSArray arrayWithContentsOfFile:path];

使用:

NSDictionary* questions = [NSDictionary dictionaryWithContentsOfFile:path];

然后要找到你想要的问题,请使用:

NSDictionary *firstQuestion = [NSDictionary dictionaryWithDictionary:[questions objectForKey:[NSString stringWithFormat:@"0"]]];