搜索

首页  >  问答  >  正文

ios - objective c 要如何正确访问 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">
<dict>
    <key>SE</key>
    <dict>
        <key>l0c0</key>
        <dict>
            <key>x</key>
            <integer>0</integer>
            <key>y</key>
            <integer>0</integer>
            <key>w</key>
            <real>320</real>
            <key>h</key>
            <real>568</real>
        </dict>
    </dict>
</dict>
</plist>

有一个这样的plist,当我尝试这样访问l0c0的时候,总会遇到错误

NSString *path = [[NSBundle mainBundle]pathForResource:@"文件名" ofType:@"plist"];
NSDictionary *modelList = [NSDictionary dictionaryWithContentsOfFile:path];
for(NSDictionary *model in modelList) {
    NSLog(@"%@", model); //只能打印出 SE
    for (NSDictionary *frame in model) { //出错
        NSLog(@"%@", frame);
    }
}

请问要如何才能正确取得l0c0xy等数据?

漂亮男人漂亮男人2728 天前799

全部回复(1)我来回复

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-31 10:38:25

    SE这个key对对应的是一个字典,你用数组遍历当然取不出,直接这样取就行了

    NSString *path = [[NSBundle mainBundle]pathForResource:@"文件名" ofType:@"plist"];
    NSDictionary *modelList = [NSDictionary dictionaryWithContentsOfFile:path];
    for(NSDictionary *model in modelList) {
        NSLog(@"%@", model); //只能打印出 SE
        NSDictionary *frame = model[@"l0c0"];
        NSLog(@"%@", frame);    
    }

    回复
    0
  • 取消回复