2017-03-17 49 views
1

我有一個這種類型的結構plist。訪問plist數組Swift XCode

Root 
    item0 Dictionary 
    village string 
    bars array 
     item0 dictionary 
      name string 
     item1 dictionary 
      name string 
     item2 dictionary 

    item1 Dictionary 
    village string 
    bars array 
     item0 dictionary 
      name string 
     item1 dictionary 
      name string 
     item2 dictionary 

在我的代碼,我想訪問的酒吧陣列特定的村莊,然後遍歷所有的字典吧陣列和輸出一個字符串值,在每個那些字典。

到目前爲止,我必須訪問plist和搶村莊是這樣的。

var villages = [AnyObject]() 
    let filePath = Bundle.main.path(forResource: "Bars", ofType: "plist") 
    if let path = filePath { 
     villages = NSArray(contentsOfFile: path) as! [AnyObject] 
    } 

從這裏我可以做些什麼來訪問我的村莊陣列來獲得在該村莊陣列中的特定索引的酒吧(陣列)?

那麼從那裏我將如何訪問bars dictionary [index] .string value?

+0

確實,我只是上傳併發布了一個鏈接,或者有沒有辦法在這裏上傳? – mocode9

回答

1
let bars = villages[index]["bars"] as! NSArray    //get specific index bars in villiages 
let name = (bars[index] as! NSDictionary)["name"] as! String //get specific index name in bars 
+0

是的,這工作!你是男人! – mocode9