Skip to content

Instantly share code, notes, and snippets.

@lbrndnr
Created January 27, 2015 16:01
Show Gist options
  • Save lbrndnr/ea8bded60e723ceb9a2e to your computer and use it in GitHub Desktop.
Save lbrndnr/ea8bded60e723ceb9a2e to your computer and use it in GitHub Desktop.
A generic -objectForKey: (e.g. NSUserDefaults, NSCache)
var dictionary = ["yolo": 9]
func objectForKey<T>(key: String) -> T? {
let obj = dictionary[key]
if let obj = obj as? T {
return obj
}
return nil
}
let obj: String? = objectForKey("yolo")
println(obj)
let value: Int? = objectForKey("yolo")
println(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment