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

2019-06-08 15:06发布

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条回答
我想做一个坏孩纸
2楼-- · 2019-06-08 15:29

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)
}
查看更多
登录 后发表回答