Untyped data-driven navigation with NavigationStack
When using a NavigationStack with data-driven navigation, we may need to navigate to views driven by different data types. One way to handle this situation would be to implement an enum type with associated values of different data types. Then, in the destination closure of the navigationDestination(for:destination:) modifier, we could have a switch statement over the different associated values, instantiating different types of views, one for each associated value. However, Apple already thought of this situation, and provides NavigationPath, a type that holds an array of type-erased data to represent the content of the navigation stack.
Getting ready
Our starting point will be the app from the previous recipe. Either duplicate the folder of the app you created in the previous recipe or download the complete code for the previous recipe from the GitHub repository.
How to do it…
- To illustrate the untyped data-drive navigation...