title: how can I cache an image in swift 5?
body: <p>I am having trouble finding a good example of how I can cache images in a table view using <code>URLSession</code> to get image from the internet (API).</p>
  <p>However, I am getting issues with the cache - Here is my code so far:</p>
  <pre class="lang-swift prettyprint-override"><code>func configure(with urlString: String, name: String, address: String, station: String) {
      
      var imageCache = NSCache&lt;UIImage, String&gt;()
      
      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()
  }
  </code></pre>
  <p><img src="https://i.stack.imgur.com/w2X5X.png" alt="screnshot of code" /></p>
  <p>errors include:</p>
  <blockquote>
  <p>Cannot convert value of type 'String' to expected argument type 'UIImage'
  Value of type 'NSCache&lt;UIImage, AnyObject&gt;' has no subscripts
  'NSCache' requires that 'String' be a class type</p>
  </blockquote>