Closed sindresorhus closed 4 years ago
Apple reply:
This is an issue which you should be able to resolve.
You can achieve this by embedding a State
or ObservableObject
within your custom DynamicViewProperty
.
So Defaults
would be a propertyWrapper that conforms to DynamicViewProperty
, and has a State
with the value it wants to track. Any time it wants to note that value has changed, it can update the state, which will trigger an update for the View that Defaults
is installed on.
See also: https://developer.apple.com/documentation/swiftui/dynamicproperty
For an example of this, see: https://github.com/sindresorhus/Plash/blob/2771c939e2047857e52cd4eea5202d1b7f5b4464/Plash/util.swift#L965-L1004
Description
Currently, only the built-in
@State
and@ObservedObject
property wrappers are able to update a SwiftUI view. This limits what we can do. As a workaround, we end up using@ObservedObject
for single values, but that means we have to access it as a propertyobject.actualValue
instead of justactualValue
. It would be very useful if there was a way to manually trigger a view update. My specific use-case is to have a property wrapper for accessing UserDefaults. Currently, I use@ObservedObject
for that.What I currently use:
And this is what I want to have:
Relevant Swift Forum discussion: https://forums.swift.org/t/custom-property-wrapper-that-updates-view/31782