对Alamofire中的成员下标的含糊不清的引用 [英] Ambiguous reference to member subscript in Alamofire

查看:65
本文介绍了对Alamofire中的成员下标的含糊不清的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Alamofire.request(videosUrl!).responseJSON { response in
        if response.result.value != nil {

            let json = response.data

            do {

                let readableJSON = try JSONSerialization.jsonObject(with: json!, options: .mutableContainers) as? JSONStandard

                if let items = readableJSON {

                    for i in 0..<items.count {

                        let item = items[i] <<< ERROR HERE

                        ...REST OF CODE HERE
                    }
                }    

             } catch {
                 print(error)
             }
         }
     }

我也有一个typealias设置:

I also have a typealias set:

typealias JSONStandard = [String:AnyObject]

我的问题是为什么我得到对成员下标的含糊引用错误?我要做的就是访问数组的正确部分。

My question is why am I getting the 'Ambiguous reference to member subscript' error? All I am trying to do is access the correct part of the array.

推荐答案

发生错误,因为<$的下标方法c $ c> Dictionary (这是您的 JSONStandard )需要:

The error happens, because subscript method of Dictionary (which is yours JSONStandard) requires:

1) String 作为关键参数,但是您传递的 i 具有 Int 类型。根据示例,您的代码所基于的示例可能应该替换

1) String as a key parameter, but you passing i which has Int type. And according to the example your code is based on you probably should replace

if let items = readableJSON {
...
}

with

if let tracks = readableJSON["tracks"] as? JSONStandard {
      if let items = tracks["items"] as? [JSONStandard] {
          ...
      }
}

在这种情况下,变为 Array ,该数组使用 Int 索引

In this case items becomes an Array which uses Int index in subscript method.

2)或因为 Dictionary 也是索引类型为的集合DictionaryIndex<键,值> 下标也可以期望 DictionaryIndex< String,AnyObject> 。在您的情况下,您可以将i替换为

2) or because Dictionary is also a collection with index type DictionaryIndex<Key, Value> subscript also can expect DictionaryIndex<String, AnyObject>. In your case you can replace

for i in 0..<items.count {

for i in items.indices {

甚至连

for (_, item) in items {

项目中的(_,项目)选择哪种取决于您的 JSON

The solution you chose depends on the structure of your JSON

这篇关于对Alamofire中的成员下标的含糊不清的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆