Component - @ObservedObject
The @ObservedObject property wrapper in SwiftUI lets a view watch an external data source for changes. When the data changes, the view updates automatically to show the latest information.
The @ObservedObject property wrapper in SwiftUI lets a view watch an external data source for changes. When the data changes, the view updates automatically to show the latest information.
ContentView
└── VStack
├── Text
└── Button@ObservedObject in a SwiftUI view?class Counter: ObservableObject {
@Published var count = 0
}
struct ContentView: View {
@ObservedObject var counter = Counter()
var body: some View {
VStack {
Text("Count: \(counter.count)")
Button("Increment") {
counter.count += 1
}
}
}
}class TimerModel: ObservableObject {
@Published var time = 0
}
struct TimerView: View {
@ObservedObject var timer: TimerModel
var body: some View {
Text("Time: \(timer.time)")
}
}Settings object and passes it to two child views. Which is the best way to share this data so both children update when settings change?Pass the test and this pattern moves unknown