Skip to content

Instantly share code, notes, and snippets.

@ARamy23
Created June 13, 2018 21:32
Show Gist options
  • Save ARamy23/0a92297c8d69b642630825e82b9f1217 to your computer and use it in GitHub Desktop.
Save ARamy23/0a92297c8d69b642630825e82b9f1217 to your computer and use it in GitHub Desktop.
protocol CodableInit {
init(data: Data) throws
}
extension CodableInit where Self: Codable {
init(data: Data) throws {
let decoder = JSONDecoder()
// I'm using snake case strategy, also Codable.
// again you can use your own decoding lib/strategy
decoder.keyDecodingStrategy = .convertFromSnakeCase
self = try decoder.decode(Self.self, from: data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment