Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created October 22, 2017 02:53
Show Gist options
  • Save azamsharp/03cc6d17b7176ac6a838c5fb2a52b423 to your computer and use it in GitHub Desktop.
Save azamsharp/03cc6d17b7176ac6a838c5fb2a52b423 to your computer and use it in GitHub Desktop.
func loadArticles(callback :([Article] -> ())) {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
let json = try! JSONSerialization.jsonObject(with: data, options: [])
let dictionary = json as! JSONDictionary
let articleDictionaries = dictionary["articles"] as! [JSONDictionary]
articles = articleDictionaries.flatMap { dictionary in
return Article(dictionary :dictionary)
}
}
DispatchQueue.main.async {
callback(articles)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment