tezvyn:

Implement SwiftUI navigation between list and detail with product ID

AI-drafted, machine-checkedSource: developer.apple.combeginner
Implement SwiftUI navigation between list and detail with product ID

Tests modern SwiftUI navigation and clean data flow. Great answer: NavigationStack with NavigationLink(value:) and navigationDestination(for:), plus a bound path for programmatic control.

WHAT THIS TESTS: Even though this is framed as a beginner task, at the senior level the interviewer is checking whether you default to the modern NavigationStack API introduced in iOS 16 or fall back to legacy patterns. They want to see that you separate routing state from view state, pass identifiers rather than heavy objects, and think about programmatic control and deep linking from the start.

A GOOD ANSWER COVERS: First, wrap the root view in a NavigationStack. Second, use the value-based NavigationLink init where the list cell receives a product ID and the link carries that value. Third, attach a navigationDestination modifier to the stack for the ID type so SwiftUI knows which view to render. Fourth, hold the navigation state in a bound array or NavigationPath, typically stored in an observable router or the list view model, so pushes and pops can be driven programmatically. Fifth, inject only the ID into the detail view and let it resolve the full product through its own dependency, keeping the list decoupled.

COMMON WRONG ANSWERS: Using NavigationView instead of NavigationStack, which triggers the old split-view behavior on iPad and lacks programmatic path control. Relying on NavigationLink(destination:isActive:) or the isPresented pattern, which breaks state restoration and deep linking. Passing the entire product model into the detail view instead of the ID, which couples the list to the detail data requirements and complicates previews and testing. Hardcoding the destination view inside the list row rather than declaring it at the stack level.

LIKELY FOLLOW-UPS: How would you support deep linking directly to a product detail? How would you clear the stack and reset to the root? What changes if the product catalog lives in a shared model actor or Core Data context? How do you handle conditional destinations when the same ID type might map to different detail views?

ONE CONCRETE EXAMPLE: Imagine a ProductListView with a view model that owns a NavigationPath named path. In the body, the list iterates over product IDs and each row contains a NavigationLink(value: id) label. The NavigationStack has a modifier navigationDestination(for: Product.ID.self) that instantiates ProductDetailView(id:). When a row is tapped, SwiftUI pushes the detail view. The view model can later call path.removeLast() or path.append(anotherId) to navigate backward or forward without any view-layer intervention.

Read the original → developer.apple.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.