Convert [String: AnyObject] to [String: Any] [closed]

六月ゝ 毕业季﹏ 提交于 2020-01-02 22:11:52

问题


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?

Thanks!


回答1:


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)
}


来源:https://stackoverflow.com/questions/28005474/convert-string-anyobject-to-string-any

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!