Hello all, I am struggling with a Predicate in SwiftData, not sure if that is not yet supported but maybe I am on the wrong path.. I have a class which contains an Array of Strings (Tags) which I want to search on, so I need to filter this array with a Predicate but I can't get it to work, any suggestions?
Here is the code:
- Class
@Model
class Tasks {
var taskTitle: String = "x"
var taskDueDate: Date = Date.now
var taskDetails: String = "x"
var taskCompleted: Bool = false
var taskTags: [String]? = []
var taskAddingDate: Date = Date.now
init(taskTitle: String, taskAddedDate: Date, taskDetails: String, taskCompleted: Bool, taskTags:[String]? = nil, taskAddingDate: Date) {
self.taskTitle = taskTitle
self.taskDueDate = taskAddedDate
self.taskDetails = taskDetails
self.taskCompleted = taskCompleted
self.taskTags = taskTags
self.taskAddingDate = taskAddingDate
}
}
Predicate I have so far (it is compiling and it runs without any problems but it is not returning anything) If I leave the searchString empty I get all the tasks, so filtering with the searchString is not woking.
init(sortSelection: SortOptionTasks, addTask: Binding<Bool>, completedTask: Binding<Bool>, searchString: String, sortOrder: [SortDescriptor<Tasks>] = []) {
self.sortSelection = sortSelection
self._addTask = addTask
self._completedTask = completedTask
_task = Query(filter: #Predicate { task in
if searchString.isEmpty {
true
} else {
task.taskTags!.contains {
$0 == searchString
} == true
}
} , sort: sortOrder)
}