Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Created May 23, 2018 04:22
Show Gist options
  • Save wh1pch81n/8f53b9b93a75e7eb48b17e1a6b541a7b to your computer and use it in GitHub Desktop.
Save wh1pch81n/8f53b9b93a75e7eb48b17e1a6b541a7b to your computer and use it in GitHub Desktop.
Helper methods that wrap the swift Codable protocol
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