搜尋

首頁  >  問答  >  主體

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 天前802

全部回覆(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
  • 取消回覆