繁体   English   中英

.plist字典访问数组?

[英].plist access array of dictionaries?

我的.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>

我正在看教程,而其他许多人都在面对这个问题,有人可以向我指出正确的方向,还是请为我修复代码?

提前致谢。

我遇到了同样的问题,我通过使用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];
   }

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

代替:

self.questions = [NSArray arrayWithContentsOfFile:path];

采用:

NSDictionary* questions = [NSDictionary dictionaryWithContentsOfFile:path];

然后转到您想要的问题,使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM