You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@propertyWrapper
public struct UserDefaultPublished<Value: Codable> {
let key: String
let container: UserDefaults
let publisher: CurrentValueSubject<Value, Never>
public var projectedValue: CurrentValueSubject<Value, Never> { return publisher }
public var wrappedValue: Value {
get {
publisher.value
}
set {
publisher.send(newValue)
container.setValue(try? PropertyListEncoder.propertyListEncoder.encode(newValue),
forKey: key)
}
}
public init(_ key: String, defaultValue: Value, container: UserDefaults = .standard) {
self.key = key
self.container = container
var value = defaultValue
if let data = container.value(forKey: key) as? Data {
let optionalValue = try? PropertyListDecoder().decode(Value.self, from: data)
value = optionalValue ?? defaultValue
}
publisher = .init(value)
}
}
I simple developer so I am trying to understand is this propertyWrapper will better approach ? or it have more pros :)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I simple developer so I am trying to understand is this
propertyWrapper
will better approach ? or it have more pros :)Beta Was this translation helpful? Give feedback.
All reactions