【问题标题】:Swift 2.2 - Cannot subscript a value type of '[Dictionary<String, AnyObject>]' with an index of type 'String'Swift 2.2 - 无法使用“String”类型的索引为“[Dictionary<String, AnyObject>]”的值类型下标
【发布时间】:2016-09-29 05:54:25
【问题描述】:

我有一个函数定义如下:

private func append(inout ret: [Dictionary<String, AnyObject>]?, element: Dictionary<String, AnyObject>) {

    if ret == nil {
        ret = [Dictionary<String, AnyObject>]()
        ret?.append(element)
        return
    }

    let retGateways = ret!["gateways"] as! [Dictionary<String, AnyObject>] // ERROR: Cannot subscript...

    // more commands...
}

ret 可以是:

[[“name”: “NameA”, “gateways”: [[“id”: “ID1”, “name”: “Name1”], [“id”: “ID2”, “name”: “Name2”]]]]

element可以

[“name”: “NameB”, “gateways”: [[“id”: “ID3”, “name”: “Name3”], [“id”: “ID4”, “name”: “Name4”]]]

我得到的错误是Cannot subscript a value type of '[Dictionary&lt;String, AnyObject&gt;]' with an index of type 'String'

我在这里引用了一些线程,但它仍然没有消除这个错误。

请告诉我怎么做。

谢谢

【问题讨论】:

    标签: arrays dictionary subscript swift2.2


    【解决方案1】:

    哦,我明白了。

    在我的函数中,retArray,而不是 Dictionary

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      相关资源
      最近更新 更多