Created
August 13, 2019 21:06
-
-
Save anilgoktas/b88899c99564a1a720b4f48cc8ffcdb6 to your computer and use it in GitHub Desktop.
Property wrapper for iCloud key-value storage (NSUbiquitousKeyValueStore)
This file contains hidden or 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
| import Foundation | |
| @propertyWrapper | |
| struct iCloudUserDefault<T> { | |
| // MARK: - Properties | |
| let key: String | |
| let defaultValue: T | |
| // MARK: - Life Cycle | |
| init(_ key: String, defaultValue: T) { | |
| self.key = key | |
| self.defaultValue = defaultValue | |
| } | |
| // MARK: - PropertyWrapper | |
| var wrappedValue: T { | |
| get { return NSUbiquitousKeyValueStore.default.object(forKey: key) as? T ?? defaultValue } | |
| set { NSUbiquitousKeyValueStore.default.set(newValue, forKey: key) } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment