Created
May 23, 2018 04:22
-
-
Save wh1pch81n/8f53b9b93a75e7eb48b17e1a6b541a7b to your computer and use it in GitHub Desktop.
Helper methods that wrap the swift Codable protocol
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 Decodable { | |
// Converts a dictionary to the desired type | |
static func decode(dictionary: [String: Any]) throws -> Self { | |
let data = try JSONSerialization.data(withJSONObject: dictionary, options: []) | |
return (try JSONDecoder().decode(Self.self, from: data)) | |
} | |
} | |
extension Encodable { | |
// Turns your object into Data | |
func encode() throws -> Data { | |
return try JSONEncoder().encode(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment