ios - .plist 访问字典数组?

标签 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>

我正在学习教程,许多其他人都遇到了这个问题,谁能指出正确的方向,或者为我修复代码?

提前致谢。

最佳答案

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

代替:

self.questions = [NSArray arrayWithContentsOfFile:path];

使用:

NSDictionary* questions = [NSDictionary dictionaryWithContentsOfFile:path];

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

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

关于ios - .plist 访问字典数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24439436/

相关文章:

ios - 上传图片到服务器时出现绿线

iOS 13 - 大标题中的变音符号导致内容跳跃和警告 UITableViewAlertForLayoutOutsideViewHierarchy

objective-c - 如何从 Mac osx 10.5 中的字体文件获取字体名称?

ios - 如何在swift 3.0中编写这段代码,特别是如何在swift3中使用宏?

ios - 从模式中选择数据后如何在 UITableViewCell 中刷新 UITextField

ios - 代码优化在 ios Swift 中不起作用?

c# - 如何在 iOS/Xamarin 上获取时间格式(24/12 小时)的设备设置

objective-c - 在 Cocoa 中捕获所有多点触控板输入

iphone - 未调用 willSendRequestForAuthenticationChallenge

ios - 数组项在 IOS 中显示错误