Last active
January 3, 2020 12:35
-
-
Save WiesnerPeti/6d9957c2a3350c98cd7497b3f92bcb8a to your computer and use it in GitHub Desktop.
Convert String-keyed dictionary to RawRepresentable-Enum-keyed dictionary
This file contains hidden or 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 Dictionary where Key == String { | |
//Generic method to convert the dictionary [String:Any] dict to a specific [enum String:Any] dict | |
func enumDict<R:RawRepresentable>(as type:R.Type) -> [R:Any] where R.RawValue == String { | |
var dict:[R:Any] = [:] | |
self.forEach{ | |
guard let k = R.init(rawValue: $0.key) else {return} | |
dict[k] = $0.value | |
} | |
return dict | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment