search

Home  >  Q&A  >  body text

How to correctly access dictionary data in plist in objective c?

<?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>

There is such a plist. When I try to access l0c0 like this, I always encounter errors

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);
    }
}

How can I correctly obtain l0c0, x, y and other data?

漂亮男人漂亮男人2758 days ago818

reply all(1)I'll reply

  • 曾经蜡笔没有小新

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

    SE This key pair corresponds to a dictionary. Of course you can’t get it out using array traversal. You can just get it like this

    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);    
    }

    reply
    0
  • Cancelreply