0
0
iOS Swiftmobile~20 mins

@EnvironmentObject for shared state in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
EnvironmentObject Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What will be displayed when the button is tapped?
Consider this SwiftUI code where a shared counter is updated using @EnvironmentObject. What text will appear after tapping the button once?
iOS Swift
class Counter: ObservableObject {
  @Published var count = 0
}

struct ContentView: View {
  @EnvironmentObject var counter: Counter
  var body: some View {
    VStack {
      Text("Count: \(counter.count)")
      Button("Increment") {
        counter.count += 1
      }
    }
  }
}
ACount: 0
BCount: 2
CApp crashes due to missing environment object
DCount: 1
Attempts:
2 left
💡 Hint
Remember that tapping the button increases the count by 1 and the view updates automatically.
lifecycle
intermediate
2:00remaining
What happens if @EnvironmentObject is not injected?
Given a SwiftUI view that uses @EnvironmentObject but the environment object is not provided in the view hierarchy, what will happen when the app runs?
iOS Swift
class UserSettings: ObservableObject {
  @Published var username = "Guest"
}

struct ProfileView: View {
  @EnvironmentObject var settings: UserSettings
  var body: some View {
    Text("User: \(settings.username)")
  }
}
AThe app crashes at runtime with a fatal error
BThe Text shows 'User: Guest' by default
CThe Text shows empty string
DThe app compiles but the view does not update
Attempts:
2 left
💡 Hint
Think about what happens if the environment object is missing when the view tries to access it.
🧠 Conceptual
advanced
2:00remaining
How does @EnvironmentObject differ from @ObservedObject?
Which statement correctly describes the difference between @EnvironmentObject and @ObservedObject in SwiftUI?
A@EnvironmentObject is injected automatically from the environment, while @ObservedObject requires manual passing of the object.
B@ObservedObject is used only for local state, @EnvironmentObject only for global constants.
C@EnvironmentObject cannot trigger view updates, @ObservedObject can.
D@ObservedObject shares data across unrelated views automatically.
Attempts:
2 left
💡 Hint
Consider how each property wrapper receives its data and how it is passed to views.
🔧 Debug
advanced
2:00remaining
Why does this view not update when the shared state changes?
Given this code, why does the Text not update when the button is tapped?
iOS Swift
class Score: ObservableObject {
  @Published var points = 0
}

struct ScoreView: View {
  var score = Score()
  var body: some View {
    VStack {
      Text("Points: \(score.points)")
      Button("Add Point") {
        score.points += 1
      }
    }
  }
}
ABecause the Button action is not called
BBecause @Published does not work with Int properties
CBecause 'score' is not marked with @StateObject or @ObservedObject, so changes don't trigger view updates
DBecause the Text view is outside the VStack
Attempts:
2 left
💡 Hint
Think about how SwiftUI detects changes in observable objects to update views.
navigation
expert
3:00remaining
How to share @EnvironmentObject across multiple views in a navigation stack?
You want to share a UserSettings object using @EnvironmentObject across several views pushed in a NavigationStack. Which code snippet correctly injects the environment object so all views can access it?
A
NavigationStack {
  ContentView().environmentObject(UserSettings())
}
B
NavigationStack {
  ContentView()
}.environmentObject(UserSettings())
C
ContentView().environmentObject(UserSettings())
NavigationStack { ContentView() }
D
NavigationStack {
  ContentView()
}
UserSettings()
Attempts:
2 left
💡 Hint
The environment object must be injected at the root of the view hierarchy that contains the navigation stack.