0

I am downloading an image and add it to my cache. That works fine, but when I want to add a value to my downloaded image in the cache, my code returns the error "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSCache 0x600001cfa3c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key XXX".

 let cache = NSCache<NSString, UIImage>()

 func downloadImage(withURL url:URL, completion: @escaping (_ image:UIImage?)->()) {
        let dataTask = URLSession.shared.dataTask(with: url) { data, responseURL, error in

            var downloadedImage:UIImage?

            if let data = data {
                downloadedImage = UIImage(data: data)
                print("downloadedImage")
            }
            DispatchQueue.main.async {
                completion(downloadedImage)
            }
            if downloadedImage != nil {

                let date = Date() // calling the date
                var calendar = Calendar.current // calling the calendar
                if let timeZone = TimeZone(identifier: "GMT-4") { 
                    calendar.timeZone = timeZone
                }
                let day = calendar.component(.day, from: date)
                
                self.cache.setObject(downloadedImage!, forKey: url.absoluteString as NSString)
                self.cache.setValue(day, forKey: url.absoluteString) // this line runs into an error
            }
            
        }
        dataTask.resume()
    }
JPJerry5
  • 107
  • 10

1 Answers1

1

The error occurs because Int is not a subclass of an NSObject

But there are two other serious issues.

  1. The cache is declared as <NSString, UIImage> so the value must be an UIImage, however day is Int.
  2. It makes no sense to overwrite the image with the integer anyway

You could append the day value to the URL string

let day = calendar.component(.day, from: date)
let key = "\(url.absoluteString)-\(day)"            
self.cache.setObject(downloadedImage!, forKey: key as NSString)
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Oh okay then I misunderstood the function. Is there a way to assign a value like an integer to a cached image? I am trying to compare the dates the images were downloaded. Thank you already! – JPJerry5 Jun 21 '20 at 08:13
  • `NSCache` works like a dictionary. It holds key value pairs of the same key type and value type. A possible solution is to include the day information as prefix or suffix of the URL string – vadian Jun 21 '20 at 08:16
  • I thought of that before. I will try that then. Thank you. – JPJerry5 Jun 21 '20 at 08:20