【问题标题】:type (string , anyobject) has no subscript member类型 (string , anyobject) 没有下标成员
【发布时间】:2016-02-13 07:15:00
【问题描述】:

我使用 dataTaskWithRequest 并获取包含两个对象的数组的 json(这些对象是键、值),我想检查两个对象中键的一个值。

这是我的代码:

let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            print("Response: \(response)")

            var jsonArray: [String:AnyObject]!

            do {
                jsonArray = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as? [String:AnyObject]
            } catch {
                print(error)
            }

            for json in jsonArray {
                print("object json reciver :",json)

//type (string , anyobject) has no subscript member
                print("state :",json["state"])

            }
        })

【问题讨论】:

  • 你所说的jsonArray不是数组,是字典。

标签: swift nsdictionary nsjsonserialization nsmutableurlrequest


【解决方案1】:

json 是一个类型为(String, AnyObject) 的变量。您不能为元组下标。

替换以下内容:

print("state :",json["state"])

与:

print("\(json.0) : \(json.1)")

【讨论】:

    【解决方案2】:

    您需要将 jsonArray 转换为 NSArray。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 2016-07-08
      • 1970-01-01
      相关资源
      最近更新 更多