Skip to content

Instantly share code, notes, and snippets.

@alexnikol
Created August 17, 2020 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexnikol/de0b6518fa9feea26e9eba648386ae00 to your computer and use it in GitHub Desktop.
Save alexnikol/de0b6518fa9feea26e9eba648386ae00 to your computer and use it in GitHub Desktop.
Generic way code after refactoring
func handleDecoding<Element: Decodable>(responseData: Data,
responseType: Element.Type,
completion: @escaping (_ apiResponse: Element?, _ error: String?) -> Void) {
do {
let apiResponse = try JSONDecoder().decode(responseType, from: responseData)
completion(apiResponse, nil)
} catch {
completion(nil, "Network Error")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment