Last active
February 13, 2022 19:31
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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