Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created October 22, 2017 02:56
Show Gist options
  • Save azamsharp/b6bbd4035a929fbb2f05c84b31e234e1 to your computer and use it in GitHub Desktop.
Save azamsharp/b6bbd4035a929fbb2f05c84b31e234e1 to your computer and use it in GitHub Desktop.
struct ArticleListViewModel {
var title :String? = "Articles"
var articles :[ArticleViewModel] = [ArticleViewModel]()
}
extension ArticleListViewModel {
init(articles :[ArticleViewModel]) {
self.articles = articles
}
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