Created
December 16, 2019 13:38
-
-
Save zunjae/2af644d1fab4baa9e978d902a36894f5 to your computer and use it in GitHub Desktop.
@propertyWrapper with optional wrappedValue
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 | |
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