Skip to content

Instantly share code, notes, and snippets.

@joshuajhomann
Last active July 1, 2018 16:32
Show Gist options
  • Save joshuajhomann/f3ac81f4797376acd020572442d98f4c to your computer and use it in GitHub Desktop.
Save joshuajhomann/f3ac81f4797376acd020572442d98f4c to your computer and use it in GitHub Desktop.
Extend Dictionary to convert to Decodable
extension Dictionary where Key: StringProtocol {
func `as`<Decoded: Decodable>(type: Decoded.Type) -> Decoded? {
return (try? JSONSerialization.data(withJSONObject: self, options: []))
.flatMap{ try? JSONDecoder().decode(Decoded.self, from: $0) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment