Skip to content

Instantly share code, notes, and snippets.

@WiesnerPeti
Last active January 3, 2020 12:35
Show Gist options
  • Save WiesnerPeti/6d9957c2a3350c98cd7497b3f92bcb8a to your computer and use it in GitHub Desktop.
Save WiesnerPeti/6d9957c2a3350c98cd7497b3f92bcb8a to your computer and use it in GitHub Desktop.
Convert String-keyed dictionary to RawRepresentable-Enum-keyed dictionary
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