I'm using the following code to do a search of an array, returning items that contain parts of the search query. This works well when a search query is a single word or part of a word:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"comment.text CONTAINS [cd] %@ ",_searchQuery];
NSArray *result = [_sourceArray filteredArrayUsingPredicate:predicate];
How can I expand my predicate to take a search query with multiple words and return results that contain all of the words in query?
For example:
- Text to search "apples and oranges are fruits"
- Query: "apple fruit"
- The current predicate will not match this query to the text.
- How to modify the predicate to match the text to search to that query?
The following code enumerates the words in
_searchQuery
creating a predicate for each, then combines them into anNSCompoundPredicate
usingAND
: