Skip to content

Instantly share code, notes, and snippets.

@AndreyPanov
Last active November 29, 2022 05:10
Show Gist options
  • Save AndreyPanov/7e0121e78908749070a4c9558a5a044b to your computer and use it in GitHub Desktop.
Save AndreyPanov/7e0121e78908749070a4c9558a5a044b to your computer and use it in GitHub Desktop.
Convert objc NSDictionary to swift dictionary
extension NSDictionary {
var swiftDictionary: [String : AnyObject] {
var swiftDictionary: [String : AnyObject] = [:]
let keys = self.allKeys.flatMap { $0 as? String }
for key in keys {
let keyValue = self.value(forKey: key) as AnyObject
swiftDictionary[key] = keyValue
}
return swiftDictionary
}
}
//in code
let d = NSDictionary()
d.swiftDictionary
@pilot34
Copy link

pilot34 commented Oct 9, 2017

let swiftDict = dict as? [String: Any]

@AndreyPanov
Copy link
Author

@pilot34 I need objects instead of Any type and it always returns non Optional type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment