dual predicate search using CoreData

I have a very simple CoreData model that has 1 entity and 2 attributes.

This code works fine:
.onChange(of: searchText) { _, text in evnts.nsPredicate = text.isEmpty ? nil :NSPredicate(format: "eventName CONTAINS %@ " , text )

but I'd like to also search with the same text string for my second attribute (which is a Date). I believe an OR is appropriate for two conditions (find either one). See attempted code below:

evnts.nsPredicate = text.isEmpty ? nil : NSPredicate(format: "(eventName CONTAINS %@) OR (dueDate CONTAINS %i) " , text )

This crashes immediately %@ does the same. Is there a way to accomplish this?

How is SwiftUI not an option below?

Answered by DTS Engineer in 827871022

NSPredicate(format: "eventDate CONTAINS %i " , text )

This won't work if eventDate is of Date type and text is a string. You will need to convert text to a Date object, and then do something like this:

let eventDatePredicate = NSPredicate(format: "eventDate == %@", dateFromText as NSDate)
let predicate = NSCompoundPredicate(ordPredicateWithSubpredicates: [eventNamepredicate, eventDatePredicate])

How to converting a string to a date is covered in DateFormatter.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

look at NSCompountPredicate

Thanks for the quick answer! But my question evolved since I wrote this:

While the predicate I created (quoted above) for searching a String works great. My other attribute is a Date, but when I try to search on the date field nothing works:

.searchable(text: $searchText) .onChange(of: searchText) { _, text in evnts.nsPredicate = text.isEmpty ? nil : NSPredicate(format: "eventDate CONTAINS %i " , text )

also %@ doesn't work either

I did read up on CompoundPredicates here: https://stackoverflow.com/questions/31708540/swift-combining-predicates But since one of my predicates doesn't work on it's own, I don't see how combining them will magically work.

NSPredicate(format: "eventDate CONTAINS %i " , text )

This won't work if eventDate is of Date type and text is a string. You will need to convert text to a Date object, and then do something like this:

let eventDatePredicate = NSPredicate(format: "eventDate == %@", dateFromText as NSDate)
let predicate = NSCompoundPredicate(ordPredicateWithSubpredicates: [eventNamepredicate, eventDatePredicate])

How to converting a string to a date is covered in DateFormatter.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks for the quick answer!

Is "dateFromText" some sort of function somewhere? The compiler says it's out of scope and I don't find it in searches.

I don't want to type the entire date in the search so the == seems not in the direction I want to go. I likely need to read more up on predicates before I'm ready to create this feature.

I was thinking that I could type any character or word in my list and it would find that. Like if any of the dates were 2022 - I could type 22 and it would find that. It seems like I'd have to convert the date back to text to do that. Then there's the problematic issue with date formatting where a search of 22 might find the 22nd of a month in addition to the year and then there's numeric months vs. spelled out in long format...

dual predicate search using CoreData
 
 
Q