title: Swift Images to Cache
body: <p>I am parsing data and checking if an image is in cache.  If yes, set it, if not set it to default and send it to cache.</p>
  
  <p>The part that sends it to cache keeps giving nil ???</p>
  
  <p>Any help would be appreciated</p>
  
  <p>Pic_STR = "<a href="https://myserver/Pictures/" rel="nofollow noreferrer">https://myserver/Pictures/</a>" + Pic + ".jpg"
                  let url = URL(string: Pic_STR)</p>
  
  <pre><code>            if let imageFromCache = imageCache.object(forKey: Pic_STR as AnyObject) as? UIImage {
                  data.photo = imageFromCache
                  return
              } else {
                  data.photo = UIImage(named: "Default")
                  let imageCache = NSCache&lt;NSString, AnyObject&gt;()
                  URLSession.shared.dataTask(with: url!) { data, response, error in
                      guard let data = data, error == nil else { return }
  
                          let imageToCache = UIImage(data: data)
                          imageCache.setObject(imageToCache!, forKey: self.Pic_STR as NSString)
                          //self.image = imageToCache
  
                      }.resume()
              }
  </code></pre>