【问题标题】:Accessing plist array Swift XCode访问 plist 数组 Swift XCode
【发布时间】:2017-08-08 10:01:55
【问题描述】:

我有一个具有这种结构的 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

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

到目前为止,我必须访问 plist 并获取村庄的是这个。

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

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

那么我将如何从那里访问条形字典[index].string 值?

【问题讨论】:

  • 是的,我只是上传并发布一个链接,还是有办法在这里上传?

标签: ios arrays swift dictionary plist


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多