使用swift在plist中循环嵌套的NSDictionary

时间:2014-10-20 19:06:22

标签: swift cgrect

我想使用保存的plist

绘制一些东西

this is the plist structure

    let path = NSBundle.mainBundle().pathForResource("draw_array", ofType: "plist");
    var dict = NSDictionary(contentsOfFile: path!);

    var lastPoint: CGPoint!;

    for (myKey,myValue) in dict {

        var row = myValue as NSDictionary;
        for (index, (key, value)) in enumerate(row) {
            lastPoint = CGPoint(x: dict[1]!["start"]["x"] as Int?, y: dict[1]!["start"]["y"] as Int?);
            println("index: \(index) key: \(key) value: \(value)")
        }


    }

我无法获得单独的x和y值,并且不断出现错误

1 个答案:

答案 0 :(得分:2)

打印特定项目:

let path = NSBundle.mainBundle().pathForResource("draw_array", ofType: "plist");
var dict = NSDictionary(contentsOfFile: path!) as [String: [String: [String: AnyObject]]];
println(dict["1"]!["start"]!["x"]!)

或循环打印

let path = NSBundle.mainBundle().pathForResource("Test", ofType: "plist");
var dict = NSDictionary(contentsOfFile: path!)

for (myKey, myValue) in dict {
    for (key, value) in myValue as NSDictionary {
        let x = value["x"] as NSNumber
        let y = value["y"] as NSNumber
        // lastPoint: CGPoint = CGPoint(x: x.integerValue, y: y.integerValue)
        println("x: \(x); y: \(y)")
    }
}