How to pass ObservedObject as parameter in SwiftUI

Issue #685

Since we have custom init in ChildView to manually set a State, we need to pass ObservedObject. In the ParentView, use underscore _ to access property wrapper type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct ChildView: View {
@ObservedObject
var store: Store

@State
private var selectedTask: AnyTask

init(store: ObservedObject<Store>) {
_selectedTask = State(initialValue: tasks.first!)
_store = store
}
}

struct ParentView: View {
@ObservedObject
var store: Store

var body: some View {
ChildView(store: _store)
}

Comments