访问plist数组Swift XCode

时间:2017-03-17 01:07:50

标签: ios arrays swift dictionary plist

我有一个这种结构的plist。

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

在我的代码中,我想访问特定村庄的bars数组,然后遍历bar数组中的所有字典,并输出每个字典中的字符串值。

到目前为止,我必须访问plist并抓住村庄。

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

从这里我将如何访问我的村庄数组以获取该村庄阵列中特定指数的条形码(数组)?

然后从那里我将如何访问bars字典[index] .string value?

1 个答案:

答案 0 :(得分: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
相关问题