Challenge - 5 Problems
EnvironmentObject Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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 } } } }
Attempts:
2 left
💡 Hint
Remember that tapping the button increases the count by 1 and the view updates automatically.
✗ Incorrect
The @EnvironmentObject 'counter' is shared and observed. When the button increments 'count', the Text updates to show the new value, which is 1 after one tap.
❓ lifecycle
intermediate2: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)") } }
Attempts:
2 left
💡 Hint
Think about what happens if the environment object is missing when the view tries to access it.
✗ Incorrect
If the environment object is not injected, SwiftUI triggers a runtime fatal error because the view expects the object to be present.
🧠 Conceptual
advanced2:00remaining
How does @EnvironmentObject differ from @ObservedObject?
Which statement correctly describes the difference between @EnvironmentObject and @ObservedObject in SwiftUI?
Attempts:
2 left
💡 Hint
Consider how each property wrapper receives its data and how it is passed to views.
✗ Incorrect
@EnvironmentObject expects the object to be provided in the environment and is injected automatically. @ObservedObject requires the object to be passed explicitly to the view.
🔧 Debug
advanced2: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 } } } }
Attempts:
2 left
💡 Hint
Think about how SwiftUI detects changes in observable objects to update views.
✗ Incorrect
The 'score' property is a plain variable, not observed by SwiftUI. Without @StateObject or @ObservedObject, changes to 'points' do not cause view updates.
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?
Attempts:
2 left
💡 Hint
The environment object must be injected at the root of the view hierarchy that contains the navigation stack.
✗ Incorrect
Injecting the environment object on the NavigationStack ensures all views inside it receive the shared object automatically.