Created
January 27, 2015 16:01
-
-
Save lbrndnr/ea8bded60e723ceb9a2e to your computer and use it in GitHub Desktop.
A generic -objectForKey: (e.g. NSUserDefaults, NSCache)
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
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