Swift:循环到字典数组(错误:Any 不可转换为 NSDictionary) [英] Swift: looping into array of dictionaries (Error: Any is not convertible to NSDictionary)

查看:44
本文介绍了Swift:循环到字典数组(错误:Any 不可转换为 NSDictionary)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在字典数组中运行 for 循环.但我收到此错误:

I'm trying to run for loop in an Array of dictionaries. But I'm getting this error:

 Any is not convertible to NSDictionary

这是我的实现:

let content:NSArray = json .object(forKey: "books") as! NSArray

for contentDic:NSDictionary in content {

        print(contentDic)

    }

如何在不出现此错误的情况下将 contentDic 转换为 Dictionary 或 NSDictionary?

How can I cast contentDic to Dictionary or NSDictionary without having this error?

非常感谢您的帮助.

推荐答案

如果你能保证你的content是一个NSDictionary的数组,你可以:

If you can guarantee that your content is an array of NSDictionary, you can:

    for contentDic:NSDictionary in content as! [NSDictionary] {
        print(contentDic)

    }

这篇关于Swift:循环到字典数组(错误:Any 不可转换为 NSDictionary)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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