Issue #926
Like AppStorage, we can make a custom UserDefaults property wrapper that conforms to DynamicProperty
@propertyWrapper
public struct UserDefault<Value>: DynamicProperty {
@State private var value: Value
let key: String
let store: UserDefaults
public init(
wrappedValue: Value,
_ key: String,
store: UserDefaults = .standard
) {
self.key = key
self.store = store
let value = (store.object(forKey: key) as? Value) ?? wrappedValue
self._value = State(wrappedValue: value)
}
public var wrappedValue: Value {
get {
value
}
nonmutating set {
store.set(newValue, forKey: key)
value = newValue
}
}
public var projectedValue: Binding<Value> {
.init(
get: { wrappedValue },
set: { wrappedValue = $0 }
)
}
}