@ObservedObject helps your app watch for changes in data from outside your view. When the data changes, the view updates automatically. This keeps your app's screen fresh without you having to refresh it manually.
@ObservedObject in iOS Swift
@ObservedObject var model: YourObservableClass
The class you observe must conform to ObservableObject.
Use @Published inside the class to mark properties that trigger updates.
count changes, the text updates.class Counter: ObservableObject { @Published var count = 0 } struct ContentView: View { @ObservedObject var counter = Counter() var body: some View { Text("Count: \(counter.count)") } }
class TimerModel: ObservableObject { @Published var seconds = 0 } struct TimerView: View { @ObservedObject var timer = TimerModel() var body: some View { Text("Seconds: \(timer.seconds)") } }
This app shows a number and a button. When you tap the button, the number goes up. The view updates automatically because it watches the Counter object with @ObservedObject.
import SwiftUI class Counter: ObservableObject { @Published var count = 0 func increment() { count += 1 } } struct ContentView: View { @ObservedObject var counter = Counter() var body: some View { VStack(spacing: 20) { Text("Count: \(counter.count)") .font(.largeTitle) Button("Increment") { counter.increment() } .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(8) } .padding() } } @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
Only use @ObservedObject for objects created outside the view or passed in. For objects created inside the view, use @StateObject.
Make sure your observable class uses @Published for properties you want to trigger view updates.
Views using @ObservedObject will redraw when the observed object changes.
@ObservedObject watches an external data object for changes.
When the data changes, the view updates automatically.
Use it to keep your UI in sync with your data model.
Practice
@ObservedObject in a SwiftUI view?Solution
Step 1: Understand the role of @ObservedObject
@ObservedObject is used to watch an external data object for changes.Step 2: Connect data changes to UI updates
When the observed object changes, the SwiftUI view automatically updates to reflect those changes.Final Answer:
To watch an external data object and update the view when it changes -> Option AQuick Check:
@ObservedObject = watch external data [OK]
- Confusing @ObservedObject with @State which owns data inside the view
- Thinking @ObservedObject creates new data instead of observing
- Using @ObservedObject for constant values
Solution
Step 1: Understand @ObservedObject declaration
@ObservedObject properties are typically declared without initialization in the view, expecting the instance to be injected.Step 2: Check syntax correctness
@ObservedObject var model: MyModel is the correct declaration; initializing it inside the view is not recommended.Final Answer:
@ObservedObject var model: MyModel -> Option AQuick Check:
Declare @ObservedObject without initialization for injection [OK]
- Initializing @ObservedObject inside the view
- Omitting the var keyword
- Using optional type without proper handling
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
}
}
}
}Solution
Step 1: Understand @Published and @ObservedObject interaction
The Counter class publishes changes to its count property.Step 2: Observe how the view updates
The ContentView observes the counter object, so when count changes, the Text updates automatically.Final Answer:
The text updates to show the incremented count each tap -> Option CQuick Check:
@Published + @ObservedObject = UI updates [OK]
- Thinking @ObservedObject alone updates UI without @Published
- Confusing @State with @ObservedObject
- Assuming UI updates require manual refresh
class TimerModel: ObservableObject {
@Published var time = 0
}
struct TimerView: View {
@ObservedObject var timer: TimerModel
var body: some View {
Text("Time: \(timer.time)")
}
}Solution
Step 1: Check class conformance
TimerModel correctly conforms to ObservableObject.Step 2: Check @ObservedObject initialization
The timer property is declared but not initialized, causing a compile error.Final Answer:
The @ObservedObject property is not initialized -> Option BQuick Check:
@ObservedObject must be initialized or injected [OK]
- Forgetting to initialize @ObservedObject
- Assuming @Published must be private
- Misreading Text string interpolation
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?Solution
Step 1: Understand data sharing with @ObservedObject
Passing the same instance to children with @ObservedObject keeps them in sync.Step 2: Evaluate other options
Creating separate instances duplicates data; @State/@Binding is for simple values; @EnvironmentObject requires environment setup.Final Answer:
Declare @ObservedObject in both child views and pass the same Settings instance from the parent -> Option DQuick Check:
Share one @ObservedObject instance for synced updates [OK]
- Creating multiple instances causing unsynced data
- Confusing @Binding with @ObservedObject for complex objects
- Using @EnvironmentObject without setup
Ready to prove it?
Pass the test and this pattern moves unknown
