将[String:AnyObject]转换为[String:Any] [英] Convert [String: AnyObject] to [String: Any]

查看:545
本文介绍了将[String:AnyObject]转换为[String:Any]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个[String:AnyObject]类型的Swift变量但是我试图调用的函数需要一个[String:Any](这将是一个字典

I have a Swift variable of type [String: AnyObject] however the function i'm trying to invoke requires a [String: Any] (This would be a Dictionary

fatal error: can't unsafeBitCast between types of different sizes

任何想法我应该在这样的情况下做些什么?

Any thoughts on what I should do in a situation like this?

谢谢!

推荐答案

这在我的操场上对我有用。不确定它是最有效的方式,特别是对于大型字典,但它可能适用于你的情况。

This worked for me in a playground. Not sure it's the most efficient way, especially for a large dictionary, but it might work for your case.

var anyObjectDict = [String: AnyObject]()

anyObjectDict.updateValue("test", forKey: "key1") 
anyObjectDict.updateValue(1.0, forKey: "key2")

var anyDict = [String: Any]()

for key in anyObjectDict.keys {
    anyDict.updateValue(anyObjectDict[key], forKey: key)
}

这篇关于将[String:AnyObject]转换为[String:Any]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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