The ObservableObject protocol helps your app watch for changes in data. When data changes, the app updates the screen automatically.
ObservableObject protocol in iOS Swift
class MyModel: ObservableObject { @Published var someData: String = "" }
Use class because ObservableObject works only with classes.
Mark properties with @Published to notify views when they change.
count changes.class Counter: ObservableObject { @Published var count = 0 }
class UserSettings: ObservableObject { @Published var username = "Guest" @Published var isLoggedIn = false }
This app shows a number and a button. When you tap the button, the number goes up. The screen updates automatically because Counter uses ObservableObject and @Published.
import SwiftUI class Counter: ObservableObject { @Published var count = 0 } struct ContentView: View { @StateObject var counter = Counter() var body: some View { VStack(spacing: 20) { Text("Count: \(counter.count)") .font(.largeTitle) Button("Increment") { counter.count += 1 } .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(8) } .padding() } } @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
Always use @StateObject in views to create and own an ObservableObject.
Use @ObservedObject when a view receives an ObservableObject from elsewhere.
Remember, only classes can conform to ObservableObject, not structs.
ObservableObject lets your app watch data changes and update UI automatically.
Mark changing properties with @Published to notify views.
Use @StateObject or @ObservedObject in views to connect data and UI.
Practice
ObservableObject protocol in SwiftUI?Solution
Step 1: Understand ObservableObject role
TheObservableObjectprotocol is designed to let a class broadcast changes to its data.Step 2: Connect data changes to UI updates
When data changes, views observing the object update automatically, keeping UI in sync.Final Answer:
To allow a class to notify views when its data changes -> Option CQuick Check:
ObservableObject = Notify views on data change [OK]
- Confusing ObservableObject with view creation
- Thinking it handles user input directly
- Assuming it manages navigation
Solution
Step 1: Identify property wrapper for change notification
Inside an ObservableObject, properties that should notify views must use@Published.Step 2: Check other options
@Stateis for views,@ObservedObjectis for view properties, and plainvardoes not notify.Final Answer:
@Published var count: Int = 0 -> Option BQuick Check:
Use @Published for ObservableObject properties [OK]
- Using @State inside ObservableObject class
- Forgetting @Published wrapper
- Confusing @ObservedObject with property wrapper
class Counter: ObservableObject {
@Published var count = 0
}
struct ContentView: View {
@StateObject var counter = Counter()
var body: some View {
Button("Tap") {
counter.count += 1
print(counter.count)
}
}
}Solution
Step 1: Understand initial value and increment
Thecountstarts at 0 and increments by 1 when the button is tapped.Step 2: Check print output after increment
After increment,countis 1, so print outputs 1.Final Answer:
1 -> Option AQuick Check:
Increment then print = 1 [OK]
- Printing before increment
- Confusing initial value with updated value
- Assuming compilation error due to ObservableObject
class TimerData: ObservableObject {
var seconds = 0
func tick() {
seconds += 1
}
}Solution
Step 1: Check property change notification
Thesecondsproperty is not marked with@Published, so changes won't notify views.Step 2: Verify other options
ObservableObject does not require NSObject inheritance, methods can't be @Published, and it must be a class.Final Answer:
Missing @Published on seconds property -> Option AQuick Check:
@Published missing = no UI update [OK]
- Thinking methods can be @Published
- Believing ObservableObject needs NSObject
- Confusing class and struct requirements
class UserData: ObservableObject {
@Published var name: String = ""
}
struct UserView: View {
// What property wrapper and type should be used here?
var userData: UserData
var body: some View {
Text(userData.name)
}
}Solution
Step 1: Understand property wrappers for ObservableObject in views
Use@ObservedObjectwhen the view receives an ObservableObject from outside and watches it.Step 2: Differentiate @StateObject and @ObservedObject
@StateObjectis for creating and owning the object inside the view;@ObservedObjectis for observing an existing object.Final Answer:
@ObservedObject var userData: UserData -> Option DQuick Check:
Observe external ObservableObject with @ObservedObject [OK]
- Using @State for ObservableObject
- Confusing @StateObject with @ObservedObject
- Trying to use @Published in views
Ready to prove it?
Pass the test and this pattern moves unknown
