Skip to content

Instantly share code, notes, and snippets.

@reejosamuel
Last active October 14, 2017 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reejosamuel/164c9c98a380aab352eb2fc684f034bc to your computer and use it in GitHub Desktop.
Save reejosamuel/164c9c98a380aab352eb2fc684f034bc to your computer and use it in GitHub Desktop.
Implementing a simple string cache
class StringCache: NSCache<NSString, String> {
static let shared = ImageCache()
func set(_ text: String, for keyString: String) {
self.setObject(text, forKey: keyString as NSString)
}
func textFrom(_ keyString: String?) -> String? {
guard let keyString = keyString else {
return nil
}
return self.object(forKey: keyString as NSString)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment