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
| extension ArticleListViewModel { | |
| func get(completion :@escaping (ArticleListViewModel) -> ()) { | |
| // Use a URL builder scheme here instead of just using the string URL | |
| let url = URL(string :"https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=0cf790498275413a9247f8b94b3843fd")! | |
| // Instead of getArticles method webservice should be generic and return the requested | |
| // object/objects of requested type | |
| self.webservice.getArticles(url: url) { articles in |
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
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| self.webservice = Webservice() | |
| self.viewModel = ArticleListViewModel(service: self.webservice) | |
| self.title = self.viewModel.title | |
| loadArticles2() | |
| } | |
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
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| self.webservice = Webservice() | |
| self.viewModel = ArticleListViewModel(service: self.webservice) | |
| self.title = self.viewModel.title | |
| loadArticles2() | |
| } | |
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 json | |
| filename = "helloworld.txt" | |
| # reading the whole file at once | |
| with open(filename) as file_object: | |
| contents = file_object.read() | |
| print(contents) |
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 SourceListViewModel : NSObject { | |
| private(set) var sourceViewModels :[SourceViewModel] = [SourceViewModel]() | |
| private var webservice :Webservice | |
| init(webservice :Webservice) { | |
| self.webservice = webservice | |
| super.init() | |
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 Source { | |
| var id :String! | |
| var name :String! | |
| var description :String! | |
| init?(dictionary :JSONDictionary) { | |
| guard let id = dictionary["id"] as? String, | |
| let name = dictionary["name"] as? String, |
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 loadSources(completion :@escaping ([Source]) -> ()) { | |
| URLSession.shared.dataTask(with: sourcesURL) { data, _, _ in | |
| if let data = data { | |
| let json = try! JSONSerialization.jsonObject(with: data, options: []) | |
| let sourceDictionary = json as! JSONDictionary | |
| let dictionaries = sourceDictionary["sources"] 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
| class SourcesTableViewController : UITableViewController { | |
| private var webservice :Webservice! | |
| private var sourceListViewModel :SourceListViewModel! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| updateUI() |
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 addSource(source: SourceViewModel) { | |
| self.sourceViewModels.append(source) | |
| } | |
| func source(at index:Int) -> SourceViewModel { | |
| return self.sourceViewModels[index] | |
| } |
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 SourceListViewModel : NSObject { | |
| private(set) var sourceViewModels :[SourceViewModel] = [SourceViewModel]() |