Guest User

Untitled

a guest
Jun 18th, 2019
52
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @propertyWrapper
  2. struct UserDefault<T> {
  3. let key: String
  4. let defaultValue: T
  5. let userDefaults: UserDefaults
  6.  
  7. init(_ key: String,
  8. defaultValue: T,
  9. userDefaults: UserDefaults) {
  10. self.key = key
  11. self.defaultValue = defaultValue
  12. self.userDefaults = userDefaults
  13. }
  14.  
  15. var value: T {
  16. get {
  17. return userDefaults.object(forKey: key) as? T ?? defaultValue
  18. }
  19. set {
  20. userDefaults.set(newValue, forKey: key)
  21. }
  22. }
  23. }
  24.  
  25. struct UserDefaultsConfig {
  26. @UserDefault var x: Bool
  27.  
  28. init(defaults: UserDefaults = .standard) {
  29. self.$x = UserDefault("FOO_FEATURE_ENABLED", defaultValue: false, userDefaults: defaults)
  30. }
  31. }
RAW Paste Data