Created
September 14, 2018 15:48
-
-
Save VinceBurn/fbf86b25c89604abcb8ea142a46b0daf to your computer and use it in GitHub Desktop.
JSONFactory Protocol Extension
This file contains 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 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