swift 如何检查导航路径中是否包含NSManagedObject值?

93ze6v8z  于 2023-02-11  发布在  Swift
关注(0)|答案(1)|浏览(104)

假设我们有两个非常基本的类:

  • 一个名为ItemNSManagedObject
  • 以及一个名为NavigationObservableObject类来处理NavigationPath
final class Navigation: ObservableObject {
    @Published var path = NavigationPath()
}

我希望在NavigationPath包含特定Item元素时禁用NavigationLink,如下例所示:

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)
    }
}

然而,令我惊讶的是,一个NavigationPath没有contains()的功能。有什么想法吗?

elcex8rz

elcex8rz1#

你换吧

@Published var path = NavigationPath()

@Published var path: [Item] = []

相关问题