One Logo Pool Ball
SwiftUI · IOS API REFERENCE

NavigationPath

struct · iOS 16.0+ · SDK 27.1

struct NavigationPath

A type-erased list of data representing the content of a navigation stack. You can manage the state of a ``NavigationStack`` by initializing the stack with a binding to a collection of data. The stack stores data items in the collection for each view on the stack. You also can read and write the collection to observe and alter the stack's state. When a stack displays views that rely on only one kind of data, you can use a standard collection, like an array, to hold the data. If you need to present different kinds of data in a single stack, use a navigation path instead. The path uses type erasure so you can manage a collection of heterogeneous elements. The path also provides the usual collection controls for adding, counting, and removing data elements. ### Serialize the path When the values you present on the navigation stack conform to the <doc://com.apple.documentation/documentation/Swift/Codable> protocol, you can use the path's ``codable`` property to get a serializable representation of the path. Use that representation to save and restore the contents of the stack. For example, you can define an <doc://com.apple.documentation/documentation/Combine/ObservableObject> that handles serializing and deserializing the path: class MyModelObject: ObservableObject { @Published var path: NavigationPath static func readSerializedData() -> Data? { // Read data representing the path from app's persistent storage. } static func writeSerializedData(_ data: Data) { // Write data representing the path to app's persistent storage. } init() { if let data = Self.readSerializedData() { do { let representation = try JSONDecoder().decode( NavigationPath.CodableRepresentation.self, from: data) self.path = NavigationPath(representation) } catch { self.path = NavigationPath() } } else { self.path = NavigationPath() } } func save() { guard let representation = path.codable else { return } do { let encoder = JSONEncoder() let data = try encoder.encode(representation) Self.writeSerializedData(data) } catch { // Handle error. } } } Then, using that object in your view, you can save the state of the navigation path when the ``Scene`` enters the ``ScenePhase/background`` state: @StateObject private var pathState = MyModelObject() @Environment(\.scenePhase) private var scenePhase var body: some View { NavigationStack(path: $pathState.path) { // Add a root view here. } .onChange(of: scenePhase) { phase in if phase == .background { pathState.save() } } }

Members (10)

method · iOS 16.0+

removeLast

TypeScript
removeLast(k?: number): void
Swift declaration
mutating func removeLast(_ k: Int = 1)

Removes values from the end of this path. - Parameters: - k: The number of values to remove. The default value is `1`. - Precondition: The input parameter `k` must be greater than or equal to zero, and must be less than or equal to the number of elements in the path.

get · iOS 16.0+

count

TypeScript
get count(): number
Swift declaration
var count: Int { get }

The number of elements in this path.

method · iOS 16.0+

append

TypeScript
append(value: number): void
Swift declaration
mutating func append<V>(_ value: V) where V : Hashable

Appends a new value to the end of this path.

method · iOS 16.0+

append

TypeScript
append68641(value: number): void
Swift declaration
mutating func append<V>(_ value: V) where V : Decodable, V : Encodable, V : Hashable

Appends a new codable value to the end of this path.

get · iOS 16.0+

codable

TypeScript
get codable(): (NavigationPath_CodableRepresentation) | null
Swift declaration
var codable: NavigationPath.CodableRepresentation? { get }

A value that describes the contents of this path in a serializable format. This value is `nil` if any of the type-erased elements of the path don't conform to the <doc://com.apple.documentation/documentation/Swift/Codable> protocol.

get · iOS 16.0+

isEmpty

TypeScript
get isEmpty(): boolean
Swift declaration
var isEmpty: Bool { get }

A Boolean that indicates whether this path is empty.

init · iOS 16.0+

init

TypeScript
static init(): NavigationPath
Swift declaration
init()

Creates a new, empty navigation path.

init · iOS 16.0+

init

TypeScript
static init69647(codable: NavigationPath_CodableRepresentation): NavigationPath
Swift declaration
init(_ codable: NavigationPath.CodableRepresentation)

Creates a new navigation path from a serializable version. - Parameters: - codable: A value describing the contents of the new path in a serializable format.

init · iOS 16.0+

init

TypeScript
static init63331(elements: Array<number>): NavigationPath
Swift declaration
init<S>(_ elements: S) where S : Sequence, S.Element : Hashable

Creates a new navigation path from the contents of a sequence. - Parameters: - elements: A sequence used to create the navigation path.

init · iOS 16.0+

init

TypeScript
static init39645(elements: Array<number>): NavigationPath
Swift declaration
init<S>(_ elements: S) where S : Sequence, S.Element : Decodable, S.Element : Encodable, S.Element : Hashable

Creates a new navigation path from the contents of a sequence that contains codable elements. - Parameters: - elements: A sequence used to create the navigation path.

Skipped declarations

No declarations were skipped in this snapshot.

All SwiftUI types →