Created
November 12, 2020 22:28
-
-
Save atierian/cf7321aab31f2a53d945cf3016826773 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
@propertyWrapper | |
struct UserDefault<Value> { | |
let key: String | |
let defaultValue: Value | |
var wrappedValue: Value { | |
get { | |
(UserDefaults.standard.object(forKey: self.key) as? Value) ?? self.defaultValue | |
} | |
set { | |
UserDefaults.standard.setValue(newValue, forKey: self.key) | |
} | |
} | |
var projectedValue: Self { | |
return self | |
} | |
func removeValue() { | |
UserDefaults.standard.removeObject(forKey: self.key) | |
} | |
} | |
struct UserDefaultValues { | |
@UserDefault(key: "MyApp.someBool", defaultValue: false) | |
static var someBool: Bool | |
@UserDefault(key: "MyApp.someString", defaultValue: "someString") | |
static var someString: String | |
} | |
_ = UserDefaultValues.someBool | |
UserDefaultValues.$someBool.removeValue() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment