ios - 访问 plist 数组 Swift XCode

标签 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]
  }

从这里我将如何访问我的 villages 数组以获取该 villages 数组中特定索引处的条(数组)?

然后我如何从那里访问 bars dictionary[index].string 值?

最佳答案

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

关于ios - 访问 plist 数组 Swift XCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42847472/

相关文章:

java - 读取文件,排序,写入另一个文件

ios - 通过降低速度逐渐减慢指尖陀螺的速度

jquery - 对数组进行排序、合并和创建唯一数组,同时保留它们的来源

ios - 在 Swift 2 中剪切数组元素

Swift 结构初始化,使另一个结构像 String

iOS 10 - AVPlayer 在播放视频时显示黑屏

iphone - 所选注释的问题

ios - 在Objective-C中需要声明哪些场景关键字 “volatile”?

ios - UITableView 在 vi​​ewForHeaderInSection 中居中一个 UIImage 和 UILabel

java - 从整数字符串中减去整数