2017-03-17 35 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