0
0
iOS Swiftmobile~20 mins

Navigation path management in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Navigation Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
navigation
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)")
      }
    }
  }
}
AnavigationPath contains one element: ["DetailView"]
BnavigationPath is empty: []
CnavigationPath contains two elements: ["DetailView", "DetailView"]
DnavigationPath contains one element: ["ContentView"]
Attempts:
2 left
💡 Hint
Think about what happens when you append a string to the navigation path once.
ui_behavior
intermediate
1: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?
AnavigationPath.clear()
BnavigationPath = nil
CnavigationPath = []
DnavigationPath.removeAll()
Attempts:
2 left
💡 Hint
Check the NavigationPath API for clearing all elements.
lifecycle
advanced
2: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)")
      }
    }
  }
}
AThe navigation path resets to empty every time the view updates.
BThe navigation path is preserved and remains unchanged.
CThe navigation path duplicates its elements on each update.
DThe navigation path causes a runtime error on view update.
Attempts:
2 left
💡 Hint
Remember how @State preserves data across view updates.
📝 Syntax
advanced
2: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?
A
@State private var navigationPath: [String] = []

NavigationStack(path: $navigationPath) { ... }
B
@State private var navigationPath = NavigationPath()

NavigationStack(path: $navigationPath) { ... }
C
@State private var navigationPath = NavigationPath()

NavigationStack(path: navigationPath) { ... }
D
@State private var navigationPath = NavigationPath()

NavigationStack(path: Binding(get: { navigationPath }, set: { navigationPath = $0 })) { ... }
Attempts:
2 left
💡 Hint
Check if the path parameter requires a binding or a value.
🔧 Debug
expert
3: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)")
      }
    }
  }
}
AMissing navigationDestination for Int type causes a crash.
BAppending an Int (42) to a NavigationPath expecting String causes a crash.
CNavigationPath cannot hold Int values, only Strings.
DThe Button action is not wrapped in DispatchQueue.main.async, causing a crash.
Attempts:
2 left
💡 Hint
Check the types used in append and navigationDestination.