Skip to content

Instantly share code, notes, and snippets.

@sisoje
Created February 4, 2019 22:49
Show Gist options
  • Save sisoje/5b015f34c5270fe8ecf26c53ad5d1175 to your computer and use it in GitHub Desktop.
Save sisoje/5b015f34c5270fe8ecf26c53ad5d1175 to your computer and use it in GitHub Desktop.
class GenericCache<H: Hashable, V: AnyObject> {
let nsCache = NSCache<AnyObject, V>()
subscript(_ h: H) -> V? {
get {
return nsCache.object(forKey: h as AnyObject)
}
set {
if let v = newValue {
nsCache.setObject(v, forKey: h as AnyObject)
} else {
nsCache.removeObject(forKey: h as AnyObject)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment