Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mrugeshtank/1afb2c18e4652b19036bc6146a046f4a to your computer and use it in GitHub Desktop.
Save mrugeshtank/1afb2c18e4652b19036bc6146a046f4a to your computer and use it in GitHub Desktop.
extension UserDefaults {
subscript<T>(key: String) -> T? {
get {
return value(forKey: key) as? T
}
set {
set(newValue, forKey: key)
}
}
subscript<T: RawRepresentable>(key: String) -> T? {
get {
if let rawValue = value(forKey: key) as? T.RawValue {
return T(rawValue: rawValue)
}
return nil
}
set {
set(newValue?.rawValue, forKey: key)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment