2014-10-20 43 views
-1

我想用保存的plist遍歷plist中嵌套的NSDictionary使用SWIFT

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的值,並保持歌廳錯誤

+0

什麼錯誤?你能提供更多信息嗎? – danielbeard 2014-10-20 20:16:40

+0

類型'Int'不符合dict [1]中的協議'NSCopying'![「start」] – Ben 2014-10-20 21:16:33

回答

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)") 
    } 
} 
+0

謝謝,Scott完美無缺 – Ben 2014-10-21 07:09:51