Created
June 13, 2018 21:32
-
-
Save ARamy23/0a92297c8d69b642630825e82b9f1217 to your computer and use it in GitHub Desktop.
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
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