Last active
April 27, 2018 21:28
-
-
Save lennet/a704d1add28070a8e6402798b1f9fbd2 to your computer and use it in GitHub Desktop.
Generic NSCache in Swift
This file contains 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 Foundation | |
class Cache<T: AnyObject>: NSCache { | |
subscript(key: AnyObject) -> T? { | |
get { | |
return objectForKey(key) | |
} | |
set { | |
if let value = newValue { | |
setObject(value, forKey: key) | |
} else { | |
removeObjectForKey(key) | |
} | |
} | |
} | |
func objectForKey(key: AnyObject) -> T? { | |
return super.objectForKey(key) as? T | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment