How do I convert NSDictionary to Dictionary?
For those who have a problem with NSDictionary
, simply use this extension:
Swift 3.0
extension NSDictionary {
var swiftDictionary: Dictionary<String, Any> {
var swiftDictionary = Dictionary<String, Any>()
for key : Any in self.allKeys {
let stringKey = key as! String
if let keyValue = self.value(forKey: stringKey){
swiftDictionary[stringKey] = keyValue
}
}
return swiftDictionary
}
}
NSDictionary
in Objective-C has always non-optional values.AnyObject
has becomeAny
in Swift 3.- Considering the first two "rules"
NSDictionary
can be bridge cast toDictionary
let post_paramsValue = post_params as Dictionary<String,Any>
If the source NSDictionary
is an optional you might use as Dictionary<String,Any>?
or as? Dictionary<String,Any>
or as! Dictionary<String,Any>
or as Dictionary<String,Any>!
depending on the actual type of the NSDictionary