Challenge - 5 Problems
Navigation Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
intermediate
2:00remaining
What is the output of this SwiftUI navigation code?
Consider this SwiftUI code snippet that manages navigation paths. What will be the value of
navigationPath after tapping the button once?iOS Swift
import SwiftUI struct ContentView: View { @State private var navigationPath = NavigationPath() var body: some View { NavigationStack(path: $navigationPath) { VStack { Button("Go to Detail") { navigationPath.append("DetailView") } Text("Current path count: \(navigationPath.count)") } .navigationDestination(for: String.self) { value in Text("You are at \(value)") } } } }
Attempts:
2 left
💡 Hint
Think about what happens when you append a string to the navigation path once.
✗ Incorrect
When the button is tapped, the string "DetailView" is appended once to the navigationPath, so it contains exactly one element.
❓ ui_behavior
intermediate1:30remaining
Which option correctly resets the navigation path in SwiftUI?
You want to reset the navigation stack to the root view by clearing the navigation path. Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
Check the NavigationPath API for clearing all elements.
✗ Incorrect
NavigationPath is a struct that supports removeAll() to clear its contents. Assigning nil or using clear() is invalid. Assigning [] is invalid because NavigationPath is not an array.
❓ lifecycle
advanced2:00remaining
What happens to the navigation path when the view is recreated?
Given this SwiftUI view with a @State navigationPath, what happens to the navigation path when the view is recreated due to a state change elsewhere?
iOS Swift
struct ContentView: View {
@State private var navigationPath = NavigationPath()
@State private var counter = 0
var body: some View {
NavigationStack(path: $navigationPath) {
VStack {
Button("Go to Detail") {
navigationPath.append("DetailView")
}
Button("Increment") {
counter += 1
}
Text("Counter: \(counter)")
}
.navigationDestination(for: String.self) { value in
Text("You are at \(value)")
}
}
}
}Attempts:
2 left
💡 Hint
Remember how @State preserves data across view updates.
✗ Incorrect
@State properties keep their values across view redraws, so navigationPath remains intact when counter changes.
📝 Syntax
advanced2:00remaining
Which option causes a compile-time error in navigation path usage?
Which of these SwiftUI code snippets will cause a compile-time error when used with NavigationStack's path binding?
Attempts:
2 left
💡 Hint
Check if the path parameter requires a binding or a value.
✗ Incorrect
NavigationStack's path parameter requires a Binding to a NavigationPath. Option C passes a value, not a binding, causing a compile error.
🔧 Debug
expert3:00remaining
Why does this navigation path code cause a runtime crash?
This SwiftUI code crashes at runtime when navigating. What is the cause?
iOS Swift
struct ContentView: View {
@State private var navigationPath = NavigationPath()
var body: some View {
NavigationStack(path: $navigationPath) {
VStack {
Button("Go to Detail") {
navigationPath.append(42)
}
}
.navigationDestination(for: String.self) { value in
Text("You are at \(value)")
}
}
}
}Attempts:
2 left
💡 Hint
Check the types used in append and navigationDestination.
✗ Incorrect
NavigationStack requires a navigationDestination for every type appended to the path. Here, 42 is an Int but no navigationDestination for Int is provided, causing a runtime crash.