Skip to content

Instantly share code, notes, and snippets.

@VinceBurn
Created September 14, 2018 15:48
Show Gist options
  • Save VinceBurn/fbf86b25c89604abcb8ea142a46b0daf to your computer and use it in GitHub Desktop.
Save VinceBurn/fbf86b25c89604abcb8ea142a46b0daf to your computer and use it in GitHub Desktop.
JSONFactory Protocol Extension
protocol JSONFactory {
static func make(jsonData: Data?) -> Self?
}
extension JSONFactory where Self: Decodable {
static func make(jsonData: Data?) -> Self? {
guard let jsonData = jsonData,
let result = try? JSONDecoder().decode(Self.self, from: jsonData) else {
return nil
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment