I'm attempting to create a custom property wrapper supported by SwiftUI, meaning that changes to the corresponding properties values would cause an update to the SwiftUI view. Here is a simplified version of what I have:
@propertyWrapper
public struct Foo: DynamicProperty {
@ObservedObject var observed: SomeObservedObject
public var wrappedValue: [SomeValue] {
return observed.value
}
}
I see that even if my ObservedObject
is contained inside of my custom property wrapper, SwiftUI still catches the changes to SomeObservedObject
as long as:
- My property wrapper is a struct
- My property wrapper conforms to
DynamicProperty
Unfortunately the docs are sparse and I have a hard time telling if this only works out of luck with the current SwiftUI implementation.
The docs of DynamicProperty
(within Xcode, not online) seem to indicate that such a property is a property that is changed from the outside causing the view to redraw, but there is no guarantee about what happens when you conform your own types to this protocol.
Can I expect this to continue working in future SwiftUI releases?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…