Last active
November 29, 2022 05:10
-
-
Save AndreyPanov/7e0121e78908749070a4c9558a5a044b to your computer and use it in GitHub Desktop.
Convert objc NSDictionary to swift dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
let swiftDict = dict as? [String: Any]