Skip to content

Instantly share code, notes, and snippets.

@farhan-syed
Last active June 9, 2018 23:53
Show Gist options
  • Save farhan-syed/9c6037eb4aa2eb58eecd445ba99f1946 to your computer and use it in GitHub Desktop.
Save farhan-syed/9c6037eb4aa2eb58eecd445ba99f1946 to your computer and use it in GitHub Desktop.
func genericFetch<T: Decodable>(urlString: String, completion: @escaping (T) -> ()) {
Alamofire.request(urlString, method: .get).validate(statusCode: 200..<300).response { response in
guard response.error == nil else {
print("error calliing on \(urlString)")
return
}
guard let data = response.data else {
print("there was an error with the data")
return
}
do {
let model = try JSONDecoder().decode(T.self, from: data)
completion(model)
} catch let jsonErr {
print("failed to decode, \(jsonErr)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment