Skip to content

Instantly share code, notes, and snippets.

@anilgoktas
Created August 13, 2019 21:06
Show Gist options
  • Select an option

  • Save anilgoktas/b88899c99564a1a720b4f48cc8ffcdb6 to your computer and use it in GitHub Desktop.

Select an option

Save anilgoktas/b88899c99564a1a720b4f48cc8ffcdb6 to your computer and use it in GitHub Desktop.
Property wrapper for iCloud key-value storage (NSUbiquitousKeyValueStore)
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