NSPredicate Contains with List - ios

I have a list of tags like item 1, item 2 and I want to use an NSPredicate to filter an array if a field matches anything in those items. This doesn't accomplish it:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY tags CONTAINS[c] %#", tag];
results = [results filteredArrayUsingPredicate:predicate];

Use the IN operator followed by the array of tags.
predicate = [NSPredicate predicateWithFormat:#"ANY tags IN %#", theTags];

Related

Filter NSArray of Arrays by One Element of Array Using NSPredicate in IOS

It is possible to filter an array of strings as follows:
NSArray *array = #[#"honda",#"toyota",#"ford"];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF contains[cd] %#",#"ford"];
NSArray *filtered = [array filteredArrayUsingPredicate:pred];
I want to search an array that contains arrays of two strings by the values for the first of the strings. So for:
NSArray *cars = #[#[#"honda",#"accord"],#[#"toyota",#"corolla"],#[#"ford",#"explorer"]];
I want to search the first dimension (honda, toyota, ford) for #"ford"
Is there a way to tell the predicate I want to search on only the first attribute and return matching elements of the array?
Well here is the pred you need.
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF[FIRST] contains[cd] %#", #"ford"];

An array of id to filter an Array via NSPredicate

The following predicate works if I only pass a single itemId but I wonder what if I have multiple itemId s, how could I able to make it work ?
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(%K == %#)", kItemId, itemId];
NSArray *filteredArray = [restaurantData.itemArray filteredArrayUsingPredicate:predicate];
You can do IN query:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"(%K IN %#)", kItemId, yourArrayOfItemId];

How to concatenate two fields for search with NSPredicate?

For example, I have object like #{#"firstName":#"aaa", #"lastName":#"bbb"}.
Now I'm trying to search with next predicate:
NSPredicate *predicateFirstName = [NSPredicate predicateWithFormat:#"firstName contains [c] %#", searchString];
NSPredicate *predicateLastName = [NSPredicate predicateWithFormat:#"lastName contains [c] %#", searchString];
NSPredicate *orPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:#[predicateFirstName, predicateLastName]];
searchUsersArray = [usersArray filteredArrayUsingPredicate:orPredicate];
I need a predicate, which will be return for search string #"aa bb" users whose first name ENDED with "aa" and lastName BEGINS with #"bb"
NSPredicate *predicateLastName = [NSPredicate predicateWithFormat:#"(firstName AND lastName contains [c] %#)", searchString]
don't work for me.
First divide the search string in the end-part and the begin-part:
NSArray *parts = [#"aa bb" componentsSeperatedByString:#" "];
Then use BEGINSWITH and ENDSWITH:
[NSPredicate predicateWithFormat:#"(firstName ENDSWITH[c] %#) AND (lastName BEGINSWITH[c] %#", parts[0], parts[1]];
Alternatively you can add a computed property to the entity concatinating the first name and last name and then search in this property.
Try like this hope it help you
NSString *matchString = [NSString stringWithFormat: #"(.*aa|bb.*)",searchText];
NSString *predicateString = #"keyword MATCHES[c] %#";
NSPredicate *predicate =[NSPredicate predicateWithFormat: predicateString, matchString];
Thanks
Use keyword BEGINSWITH instead of contains.
NSPredicate *predicateFirstName = [NSPredicate predicateWithFormat:#"firstName BEGINSWITH [c] %#", searchString];
NSPredicate *predicateLastName = [NSPredicate predicateWithFormat:#"lastName BEGINSWITH [c] %#", searchString];

Objective-C: NSPredicate Check Two Arrays

Right now, I have a working app (similar to WhatsApp) where users can do a live search using an NSTextField to add users to a group chat. Using NSPredicate, I search through an array (searchableContacts) to find any available contacts on the app, and like matches are output to a UITableView, like so:
- (void)textFieldDidChange :(UITextField *)theTextField {
[filteredArray removeAllObjects];
NSMutableArray *partPredicates = [NSMutableArray arrayWithCapacity:2];
NSPredicate *currentPartPredicate1 = [NSPredicate predicateWithFormat:#"firstName CONTAINS[cd] %#", self.searchField.text];
NSPredicate *currentPartPredicate2 = [NSPredicate predicateWithFormat:#"lastName CONTAINS[cd] %#", self.searchField.text];
[partPredicates addObject:currentPartPredicate1];
[partPredicates addObject:currentPartPredicate2];
NSPredicate *fullPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:partPredicates];
filteredArray = [[self.searchableContacts filteredArrayUsingPredicate:fullPredicate] mutableCopy];
[self.searchableMembers reloadData];
}
On the resulting tableView, users are able to select users to add to the group. Now, I have an 'addedContacts' array.
When the user goes back into the textField to search for more users to add to the group, how can I alter my NSPredicate to include available contacts (from searchableContacts) that are not already in the group (addedContacts)?
You can simplify the code you have for building the predicate to this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"firstName CONTAINS[cd] %# OR lastName CONTAINS[cd] %#", self.searchField.text, self.searchField.text];
You can adjust that to exclude the items in addedContacts using this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(NOT SELF IN %#) AND (firstName CONTAINS[cd] %# OR lastName CONTAINS[cd] %#)", self.addedContacts, self.searchField.text, self.searchField.text];

How to filter array with NSPredicate for combination of words

[filteredArray filterUsingPredicate:
[NSPredicate predicateWithFormat:#"self BEGINSWITH[cd] %#", searchText]];
filteredArray contains simple NSStrings. [hello, my, get, up, seven, etc...];
It will give all strings that begin with searchText.
But if string will be a combination of words like "my name is", and searchText = name. What would a NSPredicate look like to achieve this?
UPDATE:
And how would it have to be if i want to a result with searchText = name, but not with searchText = ame? Maybe like this:
[filteredArray filterUsingPredicate:
[NSPredicate predicateWithFormat:
#"self BEGINSWITH[cd] %# or self CONTENTS[cd] %#",
searchText, searchText]];
But it should first display the strings that begin with searchText and only after those which contain searchText.
[NSPredicate predicateWithFormat:#"self CONTAINS[cd] %#", searchText];
EDIT after expansion of question
NSArray *beginMatch = [filteredArray filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:
#"self BEGINSWITH[cd] %#", searchText]];
NSArray *anyMatch = [filteredArray filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:
#"self CONTAINS[cd] %#", searchText]];
NSMutableArray *allResults = [NSMutableArray arrayWithArray:beginMatch];
for (id obj in anyMatch) {
if (![allResults containsObject:obj]) {
[allResults addObject:obj];
}
}
filteredArray = allResults;
This will have the results in the desired order without duplicate entries.
EDIT Actually beginsWith check from start of string to search string length. if exact match found then its filtered
if u have name game tame lame
search Text : ame
filtered text would be: none
contains also check from start of string to search string length but if found start, middle or end exact search string then it is filtered.
if u have name game tame lame
search Text : ame
filtered text would be: name game tame lame because all has ame
[NSPredicate predicateWithFormat:#"self CONTAINS '%#'", searchText];

Resources