1

I'm trying to use a predicate to search 2 attributes at the same time. I initially tried a compound predicate but it would only return results if both predicates matched the string.

Basically I'm looking for something similar to this:

let predicate = NSPredicate(format: "title CONTAINS[cd] %@" || "plainTextBody CONTAINS[cd] %@", searchString, searchString)
Chris
  • 247
  • 1
  • 4
  • 17
  • 1
    Does this answer your question? [Can I apply multiple predicates to an NSFetchRequest? Would it be better to manually parse my results?](https://stackoverflow.com/questions/8129508/can-i-apply-multiple-predicates-to-an-nsfetchrequest-would-it-be-better-to-manu) – pawello2222 Sep 04 '20 at 21:39
  • Thanks @pawello2222 - I literally just figured out the answer, I was close enough in the first place and a slight tweak returned the results I was expecting. I posted the answer below just in case anyone else comes across this. – Chris Sep 04 '20 at 21:42

1 Answers1

1

So it seems I was close with my original post but it's important to keep the search terms in quotation marks and not separate them like I did in my original question. Simply using the following works perfectly:

let predicate = NSPredicate(format: "title CONTAINS[cd] %@ || plainTextBody CONTAINS[cd] %@", searchString, searchString)
Chris
  • 247
  • 1
  • 4
  • 17