I tried to use navigationstack, and it works. but couldn't print the path, and with the message: " NavigationPath(_items: SwiftUI.NavigationPath.(unknown context at $7ff84b690a28).Representation.eager([SwiftUI.(unknown context at $7ff84b6907f0).CodableItemBox<Swift.String>]), subsequentItems: [], iterationIndex: 0). "

`import SwiftUI

var toDos: [String] = [ "list1", "list2", "list3" ]

struct ContentView: View { @State private var path = NavigationPath()

var body: some View {
    
    NavigationStack(path: $path) {
        List {
            ForEach(toDos, id:\.self) { toDo in

                Button(action: {
                    path.append(toDo)
                    print(path)
                }, label: {
                    Text("go to " + toDo)
                })
            }
        }
        .navigationDestination(for: String.self) { toDo in
            Text("page for " + toDo)
        }
    }
}

}

#Preview { ContentView() }` I tried navigationlink and simulator still works fine, but cannot print anything of path

1

There are 1 best solutions below

0
On

If this is just for debugging purposes, you can use dump(path) instead of print(path).

This gives an output like this:

 ▿ SwiftUI.NavigationPath
    ▿ _items: SwiftUI.NavigationPath.(unknown context at $10a94b020).Representation.eager
        ▿ eager: 1 element
            ▿ SwiftUI.(unknown context at $10a94ade8).CodableItemBox<Swift.String> #0
                ▿ super: SwiftUI.NavigationPath_ItemBoxBase
                    - isDoubleDispatchingEqualityOperation: false
                - base: "list1"
    - subsequentItems: 0 elements
    - iterationIndex: 0

The actual elements in the path are listed under ▿ eager: 1 element. There are quite a lot of noise in the output, but at least you do see the elements being printed.

If the NavigationPath only consists of Codable elements, you can encode the path to a JSON string.

let json = try! JSONEncoder().encode(path.codable!)
print(String(data: json, encoding: .utf8)!)

This gives an output like this:

["Swift.String","\"list1\""]