Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created February 4, 2019 18:47
Show Gist options
  • Save azamsharp/2335781d35bc8d52f0a9bd119ddb44ef to your computer and use it in GitHub Desktop.
Save azamsharp/2335781d35bc8d52f0a9bd119ddb44ef to your computer and use it in GitHub Desktop.
func getArticles(with url: URL, completion: @escaping ([Article]?) -> ()) {
URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
print(error.localizedDescription)
completion(nil)
} else if let data = data {
let articlesList = try? JSONDecoder().decode(ArticleList.self, from: data)
completion(articlesList?.articles)
}
}.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment