Created
March 13, 2020 20:43
-
-
Save SwiftyAlex/91549ddfc7b77c38918408d5197fafa9 to your computer and use it in GitHub Desktop.
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
@propertyWrapper | |
struct Ubiquitous<T> { | |
private var key: String | |
private var defaultValue: T | |
private var store: NSUbiquitousKeyValueStore | |
init(key: String, defaultValue: T, store: NSUbiquitousKeyValueStore = .default) { | |
self.key = key | |
self.defaultValue = defaultValue | |
self.store = store | |
} | |
var wrappedValue: T { | |
get { | |
return store.object(forKey: key) as? T ?? defaultValue | |
} | |
set { | |
store.set(newValue, forKey: key) | |
} | |
} | |
} | |
// Usage | |
@Ubiquitous(key: "collectionscreated", defaultValue: 0) | |
var collectionsCreated: Int |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment