对Alamofire中的成员下标的含糊不清的引用

编程入门 行业动态 更新时间:2024-10-05 07:19:36
本文介绍了对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.

推荐答案

发生错误,因为 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中的成员下标的含糊不清的引用

本文发布于:2023-11-26 10:06:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不清   标的   成员   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!