I am trying to fetch objects whose date
property is within today. When I give a predicate to @Query
macro to filter data like this:
@Query(filter: #Predicate<Note> { Calendar.current.isDateInToday($0.date) })
private var notes: [Note]
I get this error
My Note model:
@Model
final class Note {
var date: Date
var title: String
init(date: Date = Date.now, title: String) {
self.date = date
self.title = title
}
}
My View:
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query(filter: #Predicate<Note> { Calendar.current.isDateInToday($0.date) })
private var notes: [Note]
@State private var newNoteTitle = ""
var body: some View {
NavigationStack {
List {
ForEach(notes) { note in
Text(note.title)
}
HStack {
TextField("New Note", text: $newNoteTitle)
Button {
modelContext.insert(Note(title: newNoteTitle))
newNoteTitle = ""
} label: {
Image(systemName: "plus.circle.fill")
}
.disabled(newNoteTitle.isEmpty)
}
}
.navigationTitle("Today")
}
}
}