Skip to content

Instantly share code, notes, and snippets.

@trilliwon
Created October 14, 2019 07:13
Show Gist options
  • Save trilliwon/d80638679028e55aeced9d23c77baf7f to your computer and use it in GitHub Desktop.
Save trilliwon/d80638679028e55aeced9d23c77baf7f to your computer and use it in GitHub Desktop.
UserDefault Swift Property Wrapper
@propertyWrapper
struct UserDefault<T> {
let key: String
let value: T
init(key: String, default value: T) {
self.key = key
self.value = value
}
var wrappedValue: T {
get { return UserDefaults.standard.value(forKey: key) as? T ?? self.value }
set { UserDefaults.standard.set(newValue, forKey: key) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment