Last active
June 9, 2018 23:53
-
-
Save farhan-syed/9c6037eb4aa2eb58eecd445ba99f1946 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
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