Bird
Raised Fist0
iOS Swiftmobile~20 mins

ObservableObject protocol in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ObservableObject Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What happens when an @Published property changes in an ObservableObject?
Consider a SwiftUI view observing an ObservableObject with an @Published property. What is the expected behavior when this property changes?
AThe view ignores the change until manually refreshed.
BThe ObservableObject stops publishing changes.
CThe app crashes due to a runtime error.
DThe view automatically updates to reflect the new property value.
Attempts:
2 left
💡 Hint
Think about how SwiftUI reacts to changes in observed data.
❓ lifecycle
intermediate
2:00remaining
When is the objectWillChange publisher triggered in ObservableObject?
In an ObservableObject, which action triggers the objectWillChange publisher to notify views?
AWhen any @Published property is about to change.
BOnly when the ObservableObject is created.
CWhen the ObservableObject is deallocated.
DWhen a method is called without changing properties.
Attempts:
2 left
💡 Hint
Focus on what causes SwiftUI to know data is changing.
📝 Syntax
advanced
2:00remaining
Identify the correct ObservableObject declaration
Which code snippet correctly declares an ObservableObject with a published property in Swift?
iOS Swift
class MyModel: ObservableObject {
  @Published var count: Int = 0
}
A
class MyModel {
  @Published var count: Int = 0
}
B
class MyModel: ObservableObject {
  @Published var count: Int = 0
}
C
struct MyModel: ObservableObject {
  @Published var count: Int = 0
}
D
class MyModel: ObservableObject {
  var count: Int = 0
}
Attempts:
2 left
💡 Hint
Remember ObservableObject must be a class and @Published marks properties to notify changes.
🔧 Debug
advanced
2:00remaining
Why does the SwiftUI view not update when ObservableObject changes?
Given this code, why does the SwiftUI view not update when the count changes? class Counter: ObservableObject { var count = 0 } struct ContentView: View { @StateObject var counter = Counter() var body: some View { Text("Count: \(counter.count)") } }
AThe Text view cannot display variables from ObservableObject.
BThe @StateObject property wrapper is used incorrectly.
CThe count property is not marked with @Published, so changes are not published.
DThe Counter class does not conform to ObservableObject.
Attempts:
2 left
💡 Hint
Check how properties notify changes to SwiftUI.
🧠 Conceptual
expert
2:00remaining
What is the role of objectWillChange in ObservableObject?
In the ObservableObject protocol, what is the purpose of the objectWillChange publisher?
AIt signals subscribers before any @Published property changes, allowing views to update.
BIt stores the current state of all @Published properties.
CIt triggers only after all properties have changed.
DIt prevents views from updating when properties change.
Attempts:
2 left
💡 Hint
Think about timing of notifications to views.

Practice

(1/5)
1. What is the main purpose of the ObservableObject protocol in SwiftUI?
easy
A. To handle user input events
B. To create a new view in SwiftUI
C. To allow a class to notify views when its data changes
D. To manage app navigation between screens

Solution

  1. Step 1: Understand ObservableObject role

    The ObservableObject protocol is designed to let a class broadcast changes to its data.
  2. Step 2: Connect data changes to UI updates

    When data changes, views observing the object update automatically, keeping UI in sync.
  3. Final Answer:

    To allow a class to notify views when its data changes -> Option C
  4. Quick Check:

    ObservableObject = Notify views on data change [OK]
Hint: ObservableObject = data change notifier for views [OK]
Common Mistakes:
  • Confusing ObservableObject with view creation
  • Thinking it handles user input directly
  • Assuming it manages navigation
2. Which of the following is the correct way to mark a property inside an ObservableObject class so that changes notify the UI?
easy
A. @State var count: Int = 0
B. @Published var count: Int = 0
C. var count: Int = 0
D. @ObservedObject var count: Int = 0

Solution

  1. Step 1: Identify property wrapper for change notification

    Inside an ObservableObject, properties that should notify views must use @Published.
  2. Step 2: Check other options

    @State is for views, @ObservedObject is for view properties, and plain var does not notify.
  3. Final Answer:

    @Published var count: Int = 0 -> Option B
  4. Quick Check:

    Use @Published for ObservableObject properties [OK]
Hint: Use @Published to notify UI from ObservableObject [OK]
Common Mistakes:
  • Using @State inside ObservableObject class
  • Forgetting @Published wrapper
  • Confusing @ObservedObject with property wrapper
3. Given this code snippet, what will be printed when the button is tapped once?
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)
    }
  }
}
medium
A. 1
B. Button tapped
C. 0
D. Compilation error

Solution

  1. Step 1: Understand initial value and increment

    The count starts at 0 and increments by 1 when the button is tapped.
  2. Step 2: Check print output after increment

    After increment, count is 1, so print outputs 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Increment then print = 1 [OK]
Hint: Increment before print means output is 1 [OK]
Common Mistakes:
  • Printing before increment
  • Confusing initial value with updated value
  • Assuming compilation error due to ObservableObject
4. Identify the error in this ObservableObject class that prevents UI updates:
class TimerData: ObservableObject {
  var seconds = 0

  func tick() {
    seconds += 1
  }
}
medium
A. Missing @Published on seconds property
B. Class must inherit from NSObject
C. tick() must be marked as @Published
D. ObservableObject requires struct, not class

Solution

  1. Step 1: Check property change notification

    The seconds property is not marked with @Published, so changes won't notify views.
  2. Step 2: Verify other options

    ObservableObject does not require NSObject inheritance, methods can't be @Published, and it must be a class.
  3. Final Answer:

    Missing @Published on seconds property -> Option A
  4. Quick Check:

    @Published missing = no UI update [OK]
Hint: Properties must be @Published to notify UI [OK]
Common Mistakes:
  • Thinking methods can be @Published
  • Believing ObservableObject needs NSObject
  • Confusing class and struct requirements
5. You want to create a SwiftUI view that updates automatically when a user's name changes. Which combination correctly connects the ObservableObject and the view?
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)
  }
}
hard
A. @StateObject var userData: UserData
B. @State var userData: UserData
C. @Published var userData: UserData
D. @ObservedObject var userData: UserData

Solution

  1. Step 1: Understand property wrappers for ObservableObject in views

    Use @ObservedObject when the view receives an ObservableObject from outside and watches it.
  2. Step 2: Differentiate @StateObject and @ObservedObject

    @StateObject is for creating and owning the object inside the view; @ObservedObject is for observing an existing object.
  3. Final Answer:

    @ObservedObject var userData: UserData -> Option D
  4. Quick Check:

    Observe external ObservableObject with @ObservedObject [OK]
Hint: Use @ObservedObject to watch external ObservableObject [OK]
Common Mistakes:
  • Using @State for ObservableObject
  • Confusing @StateObject with @ObservedObject
  • Trying to use @Published in views
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard