As for Decodable
, we're all answering with the same thing here. Initialize the Published
with a decoded Value
.
extension Published: Decodable where Value: Decodable {
public init(from decoder: Decoder) throws {
self.init(initialValue: try .init(from: decoder))
}
}
On to Encodable
…
Unlike your average property wrapper, Published
does not employ wrappedValue
. Instead, accessing a Published
value triggers a static subscript, which allows it to call objectWillChange
on the ObservableObject
when set.
Behind the scenes, your Meal.validInput
, for example, relies on this code:
Published[
_enclosingInstance: self,
wrapped: \.validInput,
storage: \._validInput
]
_enclosingInstance
is necessary for publishing changes, when set
, but all it does for get
is specify how to access the Published
, using this:
_enclosingInstance[keyPath: storageKeyPath]
wrapped
is useless for Published
.
You always need to supply the subscript with a class instance, but this "_enclosingInstance
" does not need to be an ObservableObject
.
As such, you can store the Published
via another object, and encode its stored value like this:
public extension Published {
/// The stored value of a `Published`.
/// - Note: Only useful when not having access to the enclosing class instance.
var value: Value { Storage(self).value }
private final class Storage {
init(_ published: Published) {
self.published = published
}
var value: Value {
Published[
_enclosingInstance: self,
wrapped: \.never,
storage: \.published
]
}
/// Will never be called, but is necessary to provide a `KeyPath<Value>` for the static subscript.
private var never: Value {
get { fatalError() }
set { fatalError() }
}
/// "`var`" only because the static subscript requires a `WritableKeyPath`.
/// It will never be mutated.
private var published: Published<Value>
}
}
extension Published: Encodable where Value: Encodable {
public func encode(to encoder: Encoder) throws {
try value.encode(to: encoder)
}
}
Alternatively, you could use this for the entirety of the body of Storage
. It's just not as clear about documenting how it works.
@Published private(set) var value: Value
init(_ published: Published) {
_value = published
}
Storage
will not keep a reference to the ObservableObject
, so it's only suitable for capturing values—which is all Encodable
needs. Why Apple has not provided us with a built-in solution after all this time, I have no idea.