I am having trouble finding a good example of how I can cache images in a table view using URLSession
to get image from the internet (API).
However, I am getting issues with the cache - Here is my code so far:
func configure(with urlString: String, name: String, address: String, station: String) {
var imageCache = NSCache<UIImage, String>()
loader.startAnimating()
loader.isHidden = false
guard let url = URL(string: urlString) else {
return
}
let task = URLSession.shared.dataTask(with: url) { [weak self] data, _, error in
guard let data = data, error == nil else {
return
}
let locationImage = UIImage(data: data)
imageCache.setObject(locationImage!, forKey: urlString)
imageCache[url] = locationImage
DispatchQueue.main.async {
self?.nameLabel.text = name
self?.addressLabel.text = address
self?.locationImageView.image = locationImage
self?.loader.stopAnimating()
self?.loader.isHidden = true
}
}
task.resume()
}
errors include:
Cannot convert value of type 'String' to expected argument type 'UIImage' Value of type 'NSCache<UIImage, AnyObject>' has no subscripts 'NSCache' requires that 'String' be a class type