Skip to content
tezvyn:
Menu

NavigationPath (SwiftUI)

Source: developer.apple.comHardHow cards are made

NavigationPath (SwiftUI)

NavigationPath is a type erased stack of navigation destinations that backs a SwiftUI NavigationStack, letting one path hold a mix of different hashable data types so you can push, pop and control deep, heterogeneous navigation flows programmatically.

Why it exists

SwiftUI's NavigationStack needs some ordered collection of what screen comes next to render as a stack. An Array of one concrete type works for simple flows, but real apps navigate through a mix of unrelated model types in one continuous stack, for example from a search result to a product to a specific review. NavigationPath exists to let that stack hold heterogeneous, arbitrarily typed values without forcing you into one shared enum or wrapper type for every possible destination up front.

The mental model

Think of NavigationPath as a coat check ticket stub stack rather than a labeled shelf. A labeled shelf, like a typed array, can only hold one kind of item. A stack of ticket stubs does not care what each ticket represents, it just remembers the order they were issued in and can hand back exactly what was checked in, in reverse order.

How it works

NavigationPath internally stores each pushed value in a type erased form, keeping just enough information, a hash and a way to recover the concrete type, to reconstruct it later. You append values with path.append, for example path.append(someBook), and pop with path.removeLast, or clear the whole stack at once. The NavigationStack view reads this path and, for each entry, looks for a matching navigationDestination for SomeType modifier in the view hierarchy to know which view to render for that entry's concrete type. Because NavigationPath conforms to Codable when every appended type is Codable, you can serialize the entire navigation stack to restore deep links or resume a saved session on next launch.

When it matters

It matters for any app with programmatic, deep, or state driven navigation: deep links that construct a multi screen path in one shot, push notifications that jump several levels deep, or restoring navigation state after backgrounding. The footgun is that popping to a specific screen means computing an index and truncating the path, there is no built in pop to type X helper, and mistakes cause either a crash from an out of range index or a stack that silently does not pop where expected.

A concrete example

A shopping app receives a push notification deep linking straight to a specific review screen. On launch it constructs a NavigationPath, appends the relevant Category, then the Product, then the Review, all in one line, and the NavigationStack renders the full three screen back stack immediately, so the user can tap back twice to reach the category browse screen exactly as if they had navigated there by hand.

Interview question

An app needs a single NavigationStack where users can navigate from a Category screen, to a Product screen, to a Review screen, three unrelated model types, and wants to build and restore that exact path programmatically from a push notification. Why is NavigationPath the right tool over a plain array based path?

  • a.NavigationPath is required because NavigationStack cannot be initialized with an array at all
  • b.NavigationPath can hold a single ordered stack of mixed, unrelated Hashable types and reconstruct them, while a typed array path is limited to one concrete type for the whole stackCorrect
  • c.NavigationPath automatically shows a back button while array based paths do not support back navigation
  • d.NavigationPath is only needed for tab based navigation, not stack based navigation
Why?

NavigationPath's core feature is type erasure across a single stack, which is exactly what mixing Category, Product and Review in one path requires. Option B is wrong since NavigationStack does support a plain typed array as its path for simpler, single type flows, and option C is wrong because the back button comes from NavigationStack itself, not from which path type backs it.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles