Skip to content

Instantly share code, notes, and snippets.

@SwiftyAlex
Created March 13, 2020 20:43
Show Gist options
  • Save SwiftyAlex/91549ddfc7b77c38918408d5197fafa9 to your computer and use it in GitHub Desktop.
Save SwiftyAlex/91549ddfc7b77c38918408d5197fafa9 to your computer and use it in GitHub Desktop.
@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