title: Swift 4 - How To load images using cache?
body: <p>I have UIImageView and I want to download images in cache if exist, I've used extension func.</p>
  
  <p>I have this code but not working:</p>
  
  <pre><code>extension UIImageView {
      func loadImageUsingCache (_ urlString : String) {
          let imageCache = NSCache&lt;AnyObject, AnyObject&gt;()
          if let cachedImage = imageCache.object(forKey: urlString as AnyObject) {
              self.image = cachedImage as? UIImage
              return
          }
          let url = URL(string: urlString)
          URLSession.shared.dataTask(with: url!) { (data, response, error) in
              if data != nil {
                  if let image = UIImage(data: data!) {
                      imageCache.setObject(image, forKey: urlString as AnyObject)
                      DispatchQueue.main.async(execute: {
                          self.image = image
                      })
                  }
              }
          }.resume()
      }
  }
  </code></pre>