Last active
September 21, 2016 21:44
-
-
Save feighter09/a5ef67cac7d14dc2e4fee6aefef0c026 to your computer and use it in GitHub Desktop.
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
protocol DefaultConvertible { | |
var serialized: [String : AnyObject] { get } | |
init?(dictionary: [String : AnyObject]) | |
} | |
class Defaults { | |
let userDefaults = NSUserDefaults.standardUserDefaults() | |
func save<T: DefaultConvertible>(toSave: T) | |
{ | |
userDefaults.setValue(toSave.serialized, forKey: "\(T.self)") | |
} | |
func fetch<T: DefaultConvertible>(type: T.Type) -> T? | |
{ | |
let key = "\(T.self)" | |
guard let dictionary = userDefaults.objectForKey(key) as? [String : AnyObject] | |
else { return nil } | |
return T(dictionary: dictionary) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment