Created
October 22, 2017 02:56
-
-
Save azamsharp/b6bbd4035a929fbb2f05c84b31e234e1 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
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