如何检查一个 NavigationPath 是否包含一个 NSManagedObject 值?

huangapple go评论49阅读模式
英文:

How to check if a NavigationPath contains an NSManagedObject value?

问题

不过令我惊讶的是,NavigationPath 没有 contains() 函数。请问有什么建议吗?

英文:

Suppose we have two very basic classes:

  • a NSManagedObject class called Item
  • and an 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?

答案1

得分: 0

将以下内容从英文翻译为中文:

Try changing

@Published var path = NavigationPath()

To

@Published var path: [Item] = []
英文:

Try changing

 @Published var path = NavigationPath()

To

 @Published var path: [Item] = []

huangapple
  • 本文由 发表于 2023年2月8日 11:56:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定