Suppose we have two very basic classes:
NSManagedObject
class called Item
ObservableObject
class called Navigation
to handle a NavigationPath
final class Navigation: ObservableObject {
@Published var path = NavigationPath()
}
I am looking to disable a NavigationLink
when the NavigationPath
contains a certain Item
element, as shown in this example:
struct ContentView: View {
@StateObject private var navigation = Navigation()
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], animation: .default)
private var items: FetchedResults<Item>
var body: some View {
NavigationStack(path: $navigation.path) {
List(items) { item in
NavigationLink(value: item) {
Text("Item at \(item.timestamp!.formatted())")
}
.disabled(navigation.path.contains(/* some Item */))
}
}
.environmentObject(navigation)
}
}
However to my surprise a NavigationPath
has no contains()
function. Any ideas please?
Try changing
@Published var path = NavigationPath()
To
@Published var path: [Item] = []