With proper indentation, code is much easier to read.
Code Block | // Data Model |
| class ArticlesData: NSObject { |
| var author: String? |
| var title: String? |
| var publishedAt: String? |
| var urlImage: String? |
| var urlWebsite : String? |
| } |
|
| // Network Manger |
| class ArticleManger{ |
| let website = "http ://newsapi .org/v2/everything?q=coronavirus&sortBy=popularity&apiKey=d&pageSize=50&page=2" |
| // I edited to be able to post on forum |
| |
| var articles: [ArticlesData]? = [] // holds array of model object |
| func fetchArticles() { |
| performRequest(urlString: website) |
| } |
|
| func performRequest(urlString: String){ |
| if let url = URL(string: website) { |
| let session = URLSession(configuration: .default) |
| let task = session.dataTask(with: url) { (data, response, error) in |
| if error != nil{ |
| print(error ?? 0) |
| return |
| } |
| if let data = data { |
| self.articles = self.parseData(data: data) |
| } |
| } |
| task.resume() |
| return |
| } |
| } |
|
| func parseData(data :Data)->[ArticlesData]? { |
| do { |
| let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary |
| let jsonArticles = jsonResult?["articles"] as? [AnyObject] ?? [] // gets first head of json file and converts it to dictionary |
| let article = ArticlesData() |
| for jsonArticle in jsonArticles{ // captures data and stores it in the model object |
| article.author = jsonArticle["author"] as? String |
| article.title = jsonArticle["description"] as? String |
| article.publishedAt = jsonArticle["publishedAt"] as? String |
| article.urlImage = jsonArticle["urlToImage"] as? String |
| article.urlWebsite = jsonArticle["url"] as? String |
| articles?.append(article) //put article data in the array |
| } |
| print(jsonArticles) |
| } catch { |
| print("\(error)") |
| } |
| return articles ?? [] |
| } |
| } |
|
| class LatestNewsViewController: UIViewController { |
| //Website API |
| let website = "http://newsapi.org/v2/everything?q=coronavirus&sortBy=popularity&apiKey=&pageSize=50&page=2" //Website API |
| var urlSelected = "" |
| var news = ArticleManger() |
| |
| // |
| @IBOutlet weak var table_view: UITableView! |
| // |
| override func viewDidLoad() { |
| super.viewDidLoad() |
| news.fetchArticles() |
| table_view.dataSource = self |
| } |
| |
| extension UIImageView { |
| func downloadImage( url: String){ |
| let news = ArticleManger() |
| news.performRequest(urlString: url) |
| DispatchQueue.main.async { |
| var image:Data |
| self.image = UIImage(data: image) |
| } |
| |
| } |
There are several errors here:
line 58: you need to declare the class LatestNewsViewController to conform to UITableViewDataSource protocol (otherwise, error on line 70) and implement the required delegate functions.
line 73: extension must not be inside a class.
You asked
How can I initialize the value of Image to download the images from the server ?
If I understand your code, data are loaded by performRequest. Exact ?
Code Block | func performRequest(urlString: String) { } |
loads self.articles.
What is not clear, is which article you want to use image from ?
What you get are images url (no image data at all yet):
So, if you want to get the image of first article:
Code Block | let imageOfFirst = self.articles[0].urlImage ?? !! |
Then load
var image by reading this from the url.