Skip to content

Instantly share code, notes, and snippets.

@zunjae
Created December 16, 2019 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zunjae/2af644d1fab4baa9e978d902a36894f5 to your computer and use it in GitHub Desktop.
Save zunjae/2af644d1fab4baa9e978d902a36894f5 to your computer and use it in GitHub Desktop.
@propertyWrapper with optional wrappedValue
@propertyWrapper
public struct Database<T> {
public let key: String
public var wrappedValue: T? {
get { return UserDefaults.standard.object(forKey: key) as? T }
set { UserDefaults.standard.set(newValue, forKey: key) }
}
public init(key: String) {
self.key = key
}
public init(key: String, wrappedValue: T) {
self.key = key
self.wrappedValue = wrappedValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment