Swift 3 JSON NSFastEnumerationIterator没有下标成员 [英] Swift 3 JSON NSFastEnumerationIterator has no subscript members

查看:184
本文介绍了Swift 3 JSON NSFastEnumerationIterator没有下标成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift 3和Alamofire 4.0。我能够打印出整个响应,但是我无法循环并打印出每个值。当我尝试打印下面的'标题'时,我得到一个Type'NSFastEnumerationIterator.Element'(又名'Any')没有下标成员。非常感谢任何帮助。

Im using Swift 3 and Alamofire 4.0. I am able to print out the entire response but I am having trouble looping through and printing out each value. I am getting a " Type 'NSFastEnumerationIterator.Element' (aka 'Any') has no subscript members when I try to print out 'title' below. Any help is greatly appreciated.

 Alamofire.request(url).responseJSON { response in
         if let dict = response.result.value as? Dictionary<String, AnyObject> {
            if let datas = dict["data"] as? NSArray{
                for data in datas{
                    print("DEVELOPER: \(data)")
                        if let title = data["myTitle"] as? String{
                            print(title)
                        }

                }
            }
        }
    } 


推荐答案

只需使用原生Swift 数组除非你绝对没有选择,否则使用总是 Swift原生类型。 NSArray 缺少类型信息,因此编译器无法推断出该数组包含字典。

Just use native Swift Array. Use always Swift native types unless you have absolutely no choice. NSArray lacks type information so the compiler cannot infer that the array contains dictionaries.

if let datas = dict["data"] as? [[String:Any]] {

这篇关于Swift 3 JSON NSFastEnumerationIterator没有下标成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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