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 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] |
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 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] |
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 |
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
private func loadArticles() { | |
// this url should be part of the URL builder scheme and not right inside the | |
// view controller but right now we are focused on MVVM | |
let url = URL(string: "https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=0cf790498275413a9247f8b94b3843fd")! | |
// this web service should use generic types. Again this is not part of the implementation | |
// as we are focusing on MVVM model | |
Webservice().getArticles(url: url) { articles in | |
print(articles) |
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
private var viewModel :ArticleListViewModel = ArticleListViewModel() { | |
didSet { | |
self.tableView.reloadData() | |
} | |
} |
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 |
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 ArticleViewModel { | |
var title :String | |
var description :String | |
} | |
extension ArticleViewModel { | |
init(article :Article) { | |
self.title = article.title |
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
self.viewModel.title = self.titleTextField.text! | |
self.viewModel.description = self.descriptionTextField.text! |
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
import Foundation | |
class Dynamic<T> { | |
var bind :(T) -> () = { _ in } | |
var value :T? { | |
didSet { | |
bind(value!) | |
} |
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
class AddArticleViewController : UIViewController { | |
// For this example assume they are UITextField | |
@IBOutlet weak var titleTextField :BindingTextField! | |
@IBOutlet weak var descriptionTextField :BindingTextField! | |
var viewModel :AddArticleViewModel! { | |
didSet { | |
viewModel.title.bind = { [unowned self] in self.titleTextField.text = $0 } |