Convert from NSDictionary to [String:Any?]

2019-06-23 22:54发布

I am using xmartlabs/Eureka to build an app with a dynamic form.

In order to fill the form I have to use setValues(values: [String: Any?]).

But I have the form values in an NSDictionary variable and I cannot cast it to [String:Any?].

Is there a way to convert an NSDictionary to [String:Any?] ?

2条回答
你好瞎i
2楼-- · 2019-06-23 23:16

Hope this helps:

let dict = NSDictionary()
var anyDict = [String: Any?]()

for (value, key) in dict {
    anyDict[key as! String] = value
}
查看更多
狗以群分
3楼-- · 2019-06-23 23:31

Just an example:

if let content = data["data"] as? [String:AnyObject] {
    print(content)
}

The data is a JSON object here. Use it accordingly.

查看更多
登录 后发表回答