<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/NavigationPath",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI14NavigationPathV"
  },
  "title" : "NavigationPath"
}
-->

# NavigationPath

A type-erased list of data representing the content of a navigation stack.

```
struct NavigationPath
```

## Overview

You can manage the state of a [`NavigationStack`](/documentation/SwiftUI/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`](/documentation/SwiftUI/NavigationPath/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`](/documentation/SwiftUI/Scene) enters the [`ScenePhase.background`](/documentation/SwiftUI/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()
        }
    }
}
```

## Topics

### Creating a navigation path

[`init()`](/documentation/SwiftUI/NavigationPath/init())

Creates a new, empty navigation path.

[`init ( _ :)`](/documentation/SwiftUI/NavigationPath/init(_:))

Creates a new navigation path from a serializable version.

### Managing path contents

[`isEmpty`](/documentation/SwiftUI/NavigationPath/isEmpty)

A Boolean that indicates whether this path is empty.

[`count`](/documentation/SwiftUI/NavigationPath/count)

The number of elements in this path.

[`func   append ( _ :)`](/documentation/SwiftUI/NavigationPath/append(_:))

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

[`removeLast(_:)`](/documentation/SwiftUI/NavigationPath/removeLast(_:))

Removes values from the end of this path.

### Encoding a path

[`codable`](/documentation/SwiftUI/NavigationPath/codable)

A value that describes the contents of this path in a serializable
format.

[`CodableRepresentation`](/documentation/SwiftUI/NavigationPath/CodableRepresentation)

A serializable representation of a navigation path.

## Relationships

### Conforms To

[`Equatable`](/documentation/Swift/Equatable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)