Last active
July 1, 2018 16:32
-
-
Save joshuajhomann/f3ac81f4797376acd020572442d98f4c to your computer and use it in GitHub Desktop.
Extend Dictionary to convert to Decodable
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 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