Bird
Raised Fist0
iOS Swiftmobile~20 mins

@ObservedObject in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ObservedObject Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
How does @ObservedObject update the UI?
Consider a SwiftUI view using an @ObservedObject to watch a data model. What happens when a published property in the observed object changes?
iOS Swift
class Counter: ObservableObject {
  @Published var count = 0
}

struct ContentView: View {
  @ObservedObject var counter = Counter()
  var body: some View {
    Text("Count: \(counter.count)")
  }
}
AThe app crashes because @ObservedObject cannot be used with classes.
BThe Text view does not update unless the view is recreated manually.
CThe Text view updates automatically to show the new count value.
DThe Text view updates only if the count property is marked with @State.
Attempts:
2 left
💡 Hint
Think about how SwiftUI listens to changes in ObservableObject classes.
❓ lifecycle
intermediate
2:00remaining
When is an @ObservedObject recreated in SwiftUI?
Given a SwiftUI view with an @ObservedObject property initialized inside the view struct, when does the observed object get recreated?
iOS Swift
class TimerModel: ObservableObject {
  @Published var time = 0
}

struct TimerView: View {
  @ObservedObject var timer = TimerModel()
  var body: some View {
    Text("Time: \(timer.time)")
  }
}
AThe TimerModel instance is created once and reused across redraws.
BEvery time the view redraws, a new TimerModel instance is created.
CThe TimerModel instance is never created because @ObservedObject requires external injection.
DThe TimerModel instance is created only when the app launches.
Attempts:
2 left
💡 Hint
Think about how SwiftUI structs are recreated and what happens to properties initialized inside them.
🔧 Debug
advanced
2:00remaining
Why does the UI not update when using @ObservedObject?
You have this code but the UI does not update when the data changes. What is the most likely cause?
iOS Swift
class DataModel: ObservableObject {
  var value = 0
}

struct ContentView: View {
  @ObservedObject var model = DataModel()
  var body: some View {
    Text("Value: \(model.value)")
  }
}
AThe value property is not marked with @Published, so changes are not announced.
BThe DataModel class must inherit from NSObject for @ObservedObject to work.
CThe Text view must be inside a VStack to update properly.
DThe @ObservedObject property wrapper must be replaced with @State for updates.
Attempts:
2 left
💡 Hint
Check how SwiftUI knows when to update views based on ObservableObject changes.
❓ navigation
advanced
2:00remaining
Passing @ObservedObject between views
You want to share a data model between two SwiftUI views so both update when the model changes. Which approach correctly shares the @ObservedObject?
iOS Swift
class UserSettings: ObservableObject {
  @Published var username = "Guest"
}

struct ParentView: View {
  @StateObject var settings = UserSettings()
  var body: some View {
    ChildView(settings: settings)
  }
}

struct ChildView: View {
  @ObservedObject var settings: UserSettings
  var body: some View {
    Text("User: \(settings.username)")
  }
}
AParentView creates the UserSettings with @StateObject and passes it to ChildView as @ObservedObject.
BUserSettings should be passed as a plain variable without property wrappers.
CChildView should use @StateObject to create UserSettings instead of @ObservedObject.
DBoth ParentView and ChildView create their own UserSettings with @ObservedObject.
Attempts:
2 left
💡 Hint
Think about which view owns the data and which views observe it.
🧠 Conceptual
expert
2:00remaining
Difference between @ObservedObject and @StateObject
Which statement best describes the difference between @ObservedObject and @StateObject in SwiftUI?
A@StateObject is used only for structs; @ObservedObject is used only for classes.
B@ObservedObject creates and owns the ObservableObject instance; @StateObject only observes an existing instance.
CBoth @ObservedObject and @StateObject create new instances every time the view redraws.
D@StateObject creates and owns the ObservableObject instance; @ObservedObject only observes an existing instance.
Attempts:
2 left
💡 Hint
Consider which property wrapper manages the lifecycle of the object.

Practice

(1/5)
1. What is the main purpose of using @ObservedObject in a SwiftUI view?
easy
A. To watch an external data object and update the view when it changes
B. To create a new data object inside the view
C. To store a constant value that never changes
D. To handle user input events like taps and swipes

Solution

  1. Step 1: Understand the role of @ObservedObject

    @ObservedObject is used to watch an external data object for changes.
  2. Step 2: Connect data changes to UI updates

    When the observed object changes, the SwiftUI view automatically updates to reflect those changes.
  3. Final Answer:

    To watch an external data object and update the view when it changes -> Option A
  4. Quick Check:

    @ObservedObject = watch external data [OK]
Hint: Remember: @ObservedObject watches data outside the view [OK]
Common Mistakes:
  • Confusing @ObservedObject with @State which owns data inside the view
  • Thinking @ObservedObject creates new data instead of observing
  • Using @ObservedObject for constant values
2. Which of the following is the correct way to declare an observed object in a SwiftUI view?
easy
A. @ObservedObject var model: MyModel
B. @ObservedObject var model: MyModel = MyModel()
C. @ObservedObject model = MyModel()
D. @ObservedObject var model: MyModel?

Solution

  1. Step 1: Understand @ObservedObject declaration

    @ObservedObject properties are typically declared without initialization in the view, expecting the instance to be injected.
  2. Step 2: Check syntax correctness

    @ObservedObject var model: MyModel is the correct declaration; initializing it inside the view is not recommended.
  3. Final Answer:

    @ObservedObject var model: MyModel -> Option A
  4. Quick Check:

    Declare @ObservedObject without initialization for injection [OK]
Hint: Declare @ObservedObject properties without initializing inside the view [OK]
Common Mistakes:
  • Initializing @ObservedObject inside the view
  • Omitting the var keyword
  • Using optional type without proper handling
3. Given the code below, what will be the output when the button is tapped?
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
      }
    }
  }
}
medium
A. The app crashes due to missing @State
B. The text never updates and stays at 0
C. The text updates to show the incremented count each tap
D. The button does not respond to taps

Solution

  1. Step 1: Understand @Published and @ObservedObject interaction

    The Counter class publishes changes to its count property.
  2. Step 2: Observe how the view updates

    The ContentView observes the counter object, so when count changes, the Text updates automatically.
  3. Final Answer:

    The text updates to show the incremented count each tap -> Option C
  4. Quick Check:

    @Published + @ObservedObject = UI updates [OK]
Hint: @Published properties trigger view updates via @ObservedObject [OK]
Common Mistakes:
  • Thinking @ObservedObject alone updates UI without @Published
  • Confusing @State with @ObservedObject
  • Assuming UI updates require manual refresh
4. Identify the error in this SwiftUI view using @ObservedObject:
class TimerModel: ObservableObject {
  @Published var time = 0
}

struct TimerView: View {
  @ObservedObject var timer: TimerModel
  var body: some View {
    Text("Time: \(timer.time)")
  }
}
medium
A. The TimerModel class is missing ObservableObject conformance
B. The @ObservedObject property is not initialized
C. The @Published property should be private
D. The Text view syntax is incorrect

Solution

  1. Step 1: Check class conformance

    TimerModel correctly conforms to ObservableObject.
  2. Step 2: Check @ObservedObject initialization

    The timer property is declared but not initialized, causing a compile error.
  3. Final Answer:

    The @ObservedObject property is not initialized -> Option B
  4. Quick Check:

    @ObservedObject must be initialized or injected [OK]
Hint: Always initialize or inject @ObservedObject properties [OK]
Common Mistakes:
  • Forgetting to initialize @ObservedObject
  • Assuming @Published must be private
  • Misreading Text string interpolation
5. You have a parent view that creates a 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?
hard
A. Use @EnvironmentObject in children without passing from parent
B. Create separate @ObservedObject instances in each child view
C. Use @State in the parent and @Binding in children for the settings
D. Declare @ObservedObject in both child views and pass the same Settings instance from the parent

Solution

  1. Step 1: Understand data sharing with @ObservedObject

    Passing the same instance to children with @ObservedObject keeps them in sync.
  2. Step 2: Evaluate other options

    Creating separate instances duplicates data; @State/@Binding is for simple values; @EnvironmentObject requires environment setup.
  3. Final Answer:

    Declare @ObservedObject in both child views and pass the same Settings instance from the parent -> Option D
  4. Quick Check:

    Share one @ObservedObject instance for synced updates [OK]
Hint: Pass one @ObservedObject instance to children for shared updates [OK]
Common Mistakes:
  • Creating multiple instances causing unsynced data
  • Confusing @Binding with @ObservedObject for complex objects
  • Using @EnvironmentObject without setup
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