Skip to content

Instantly share code, notes, and snippets.

@adamzarn
Last active February 13, 2022 19:31
Show Gist options
  • Save adamzarn/335d1f0226eb252f353d5eebdac0cb1e to your computer and use it in GitHub Desktop.
Save adamzarn/335d1f0226eb252f353d5eebdac0cb1e to your computer and use it in GitHub Desktop.
A singleton for managing a cache of images identified by their url strings.
import UIKit
class ImageCache: NSCache<NSString, UIImage> {
static let shared = ImageCache()
subscript(key: String) -> UIImage? {
get {
return object(forKey: NSString(string: key))
}
set {
if let newValue = newValue {
setObject(newValue, forKey: NSString(string: key))
} else {
removeObject(forKey: NSString(string: key))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment