Store Search - Caching images using UIImageView Extension

Hi there!

I’m trying to to add caching to the UIImageView+DownloadImage extension as mentioned on page 125. I’ve been at this for two days and can’t figure it. I can’t find any examples of how to configure URLSessionConfiguration settings. Everything I’ve found has been written in Swift 2. The code below is one of many attempts at it. Can you point to a solid example, or provide some advice on this? Thanks!!

let imageCache = URLCache()

extension UIImageView {
    func loadImageWithURL(_ myString: String) -> Void {

        let myNSString = myString as! NSString
        
        if let cachedImage = imageCache.value(forKey: myNSString) as? UIImage {
            self.image = cachedImage
            } else {
            return
        }
 
        
        guard let postURL: URL = URL(string: myString) else {
            return
        }
        
  //      let session = URLSession.shared
        let request = NSMutableURLRequest(url: postURL)
 //        request.cachePolicy = NSURLRequest.CachePolicy.returnCacheDataElseLoad
        
        let downloadTask = session.downloadTask(with: request as URLRequest, completionHandler: { [weak self] url, response, error in
        
              if error == nil, let url = url,
                let data = try? Data(contentsOf: url), let image = UIImage(data: data) {
                imageCache.
                    imageCache.setValue(image, forKey: (myNSString as! NSString) as String)
                
                
                    DispatchQueue.main.async {
                        if let strongSelf = self {
                            strongSelf.image = image
                        }
                    }
            }
        })
        downloadTask.resume()
       // return downloadTask
    }
}