I'm trying to add downloaded images to imageViews in a tableView cell. The images that are downloaded have urls in an array "articles".
I have written a function which downloads the image depending on the urls stored in an array:
let urlRequest = URLRequest(url: URL(string: url)!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
if error != nil {
print("error")
return
}
DispatchQueue.main.async {
self.image = UIImage(data: data!)
}
}
task.resume()
}
}
Then, in the cellForRowAtIndexPath method :
var imageData = self.articles?[indexPath.item].imageUrl
cell.imgView.downloadImage(from: imageData!)
However, this gives me the error: "Unexpectedly found nil while unwrapping an Optional value" in the line:
cell.imgView.downloadImage(from: imageData!)
How do you fix this problem?