英文:
How to check if a NavigationPath contains an NSManagedObject value?
问题
不过令我惊讶的是,NavigationPath
没有 contains()
函数。请问有什么建议吗?
英文:
Suppose we have two very basic classes:
- a
NSManagedObject
class calledItem
- and an
ObservableObject
class calledNavigation
to handle aNavigationPath
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] = []
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论