使用 NSPredicate 过滤/取消过滤 SectionedFetchRequest 中的部分
Using NSPredicate to filter / unfilter sections in a SectionedFetchRequest
我正在使用 SectionedFetchRequest
对我的数据进行排序并提取到部分中。我构建了一个过滤器按钮,它应该过滤优先级为 0 的所有数据(它被保存为 int32
并更新 list
.
阅读 Apple's Predicate Format Syntax 后,我尝试了以下方法:
sections.nsPredicate = NSPredicate(format: "priorityText CONTAINS[c] %@", "Today")
sections.nsPredicate = NSPredicate(format: "priority == %@", 0) //**
sections.nsPredicate = NSPredicate(format: "priority like %@", 0)
sections.nsPredicate = NSPredicate(format: "priorityText == %@", "Today")
sections.nsPredicate = NSPredicate(format: "priorityText like %@", "Today")
sections.nsPredicate = NSPredicate(format: "priority == %@", 0 as NSInteger) //**
sections.nsPredicate = NSPredicate(format: "priority == %@", 0 as Int32) //**
评论//**
的没有崩溃,全部过滤掉了
我去 SO(比如 this one) and other sources (and this and this about predicateWithFortmat)阅读了一些问题,试图找出如何过滤掉整数。但我尝试的任何一个都不起作用。
此外,我不知道如何取消过滤数据...
实体
我的代码:
extension Task {
@objc
var priorityText: String{
var result = ""
switch self.priority {
case 0: result = "Today"
case 1: result = "Important Tasks"
default: result = "All Others"
}
return result
}
}
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@SectionedFetchRequest(entity: Task.entity(),
sectionIdentifier: \.priorityText,
sortDescriptors: [NSSortDescriptor(keyPath: \Task.priority, ascending: true)],
predicate: nil,
animation: Animation.linear)
var sections: SectionedFetchResults<String, Task>
@State var filtered = false
var body: some View {
NavigationView {
List {
ForEach(sections) { section in
Section(header: Text(section.id.capitalized).fontWeight(.bold).foregroundColor(.primary)) {
ForEach(section) { task in
Text("\(task.tasktext!)")
}
}
}
}
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button(action: {
filtered.toggle()
if filtered {
// check https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html
//sections.nsPredicate = NSPredicate(format: "priorityText CONTAINS[c] %@", "Today")
//sections.nsPredicate = NSPredicate(format: "priority == %@", 0)
//sections.nsPredicate = NSPredicate(format: "priority like %@", 0)
//sections.nsPredicate = NSPredicate(format: "priorityText == %@", "Today")
//sections.nsPredicate = NSPredicate(format: "priorityText like %@", "Today")
sections.nsPredicate = NSPredicate(format: "priority == %@", 0 as NSInteger)
} else {
print("unfilter?")
}
}) {
Label("Filter", systemImage: filtered ? "line.3.horizontal.decrease.circle.fill": "line.3.horizontal.decrease.circle").foregroundColor(.primary)
}
}
}
.listStyle(.plain)
}
}
}
编辑:
第一个问题(如何根据整数筛选数据)的答案是:
sections.nsPredicate = NSPredicate(format: "priority == %i",0)
并显示未过滤的数据:
sections.nsPredicate = NSPredicate(format: "priority >= %i",0)
我要回答我自己的问题:
要筛选:
sections.nsPredicate = NSPredicate(format: "priority == %i",0)
取消过滤:
sections.nsPredicate = NSPredicate(format: "priority >= %i",0)
我正在使用 SectionedFetchRequest
对我的数据进行排序并提取到部分中。我构建了一个过滤器按钮,它应该过滤优先级为 0 的所有数据(它被保存为 int32
并更新 list
.
阅读 Apple's Predicate Format Syntax 后,我尝试了以下方法:
sections.nsPredicate = NSPredicate(format: "priorityText CONTAINS[c] %@", "Today")
sections.nsPredicate = NSPredicate(format: "priority == %@", 0) //**
sections.nsPredicate = NSPredicate(format: "priority like %@", 0)
sections.nsPredicate = NSPredicate(format: "priorityText == %@", "Today")
sections.nsPredicate = NSPredicate(format: "priorityText like %@", "Today")
sections.nsPredicate = NSPredicate(format: "priority == %@", 0 as NSInteger) //**
sections.nsPredicate = NSPredicate(format: "priority == %@", 0 as Int32) //**
评论//**
的没有崩溃,全部过滤掉了
我去 SO(比如 this one) and other sources (and this and this about predicateWithFortmat)阅读了一些问题,试图找出如何过滤掉整数。但我尝试的任何一个都不起作用。
此外,我不知道如何取消过滤数据...
实体
我的代码:
extension Task {
@objc
var priorityText: String{
var result = ""
switch self.priority {
case 0: result = "Today"
case 1: result = "Important Tasks"
default: result = "All Others"
}
return result
}
}
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@SectionedFetchRequest(entity: Task.entity(),
sectionIdentifier: \.priorityText,
sortDescriptors: [NSSortDescriptor(keyPath: \Task.priority, ascending: true)],
predicate: nil,
animation: Animation.linear)
var sections: SectionedFetchResults<String, Task>
@State var filtered = false
var body: some View {
NavigationView {
List {
ForEach(sections) { section in
Section(header: Text(section.id.capitalized).fontWeight(.bold).foregroundColor(.primary)) {
ForEach(section) { task in
Text("\(task.tasktext!)")
}
}
}
}
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button(action: {
filtered.toggle()
if filtered {
// check https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html
//sections.nsPredicate = NSPredicate(format: "priorityText CONTAINS[c] %@", "Today")
//sections.nsPredicate = NSPredicate(format: "priority == %@", 0)
//sections.nsPredicate = NSPredicate(format: "priority like %@", 0)
//sections.nsPredicate = NSPredicate(format: "priorityText == %@", "Today")
//sections.nsPredicate = NSPredicate(format: "priorityText like %@", "Today")
sections.nsPredicate = NSPredicate(format: "priority == %@", 0 as NSInteger)
} else {
print("unfilter?")
}
}) {
Label("Filter", systemImage: filtered ? "line.3.horizontal.decrease.circle.fill": "line.3.horizontal.decrease.circle").foregroundColor(.primary)
}
}
}
.listStyle(.plain)
}
}
}
编辑:
第一个问题(如何根据整数筛选数据)的答案是:
sections.nsPredicate = NSPredicate(format: "priority == %i",0)
并显示未过滤的数据:
sections.nsPredicate = NSPredicate(format: "priority >= %i",0)
我要回答我自己的问题:
要筛选:
sections.nsPredicate = NSPredicate(format: "priority == %i",0)
取消过滤:
sections.nsPredicate = NSPredicate(format: "priority >= %i",0)