【问题标题】:Cycling element in array of dictionary字典数组中的循环元素
【发布时间】:2016-01-05 05:27:20
【问题描述】:

我有这个代码:

let jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray

                for element in jsonResults {
                    for (chiave,valore) in element {
                        print("la chiave\(chiave) è uguale a \(valore)")
                    }
                }

我在这一行有一个错误:

for (chiave,valore) in element {

Type element Aka AnyObject 不符合协议'Sequence Type'

jsonResults 是一个数组

元素是字典

我可以将元素转换为字典吗?

数组类似这样:

[0] : 92 elements
    ▿ [0] : 2 elements
      - .0 : NOME
      - .1 : MARIO
    ▿ [1] : 2 elements
      - .0 : CAP_DOMICILIO
      - .1 : 000000
    ▿ [2] : 2 elements
      - .0 : DATA_ULTIMO_AGG
      - .1 : 2015-09-22 

【问题讨论】:

标签: ios arrays swift dictionary for-in-loop


【解决方案1】:

我会将 Element 转换为 [String:AnyObject]

类似

            if let jsonResults = jsonResults as? [[String: AnyObject]] {
                for element in jsonResults {
                    //Do stuff
                }
            }

【讨论】:

  • 谢谢!这是正确的,但 swift 2 想要...作为? ([[String: AnyObject]])
猜你喜欢
  • 2015-01-31
  • 2017-12-10
  • 2023-01-25
  • 2019-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 2020-10-09
相关资源
最近更新 更多