Skip to content

Instantly share code, notes, and snippets.

@Otbivnoe
Created March 16, 2018 06:35
Show Gist options
  • Save Otbivnoe/152fa03f561ad54419da016ec772eafa to your computer and use it in GitHub Desktop.
Save Otbivnoe/152fa03f561ad54419da016ec772eafa to your computer and use it in GitHub Desktop.
extension UserDefaults {
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)
}
}
}
class SettingsService {
var appTheme: AppTheme {
get {
return UserDefaults.standard[#function] ?? .light
}
set {
UserDefaults.standard[#function] = newValue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment