I have an array of Address Book contact dictionaries, with the dictionary for each name containing a string for the name and an array of email addresses. Here's a snippet of what the NSLog output looks like when I log the array of contacts:
{
emails = (
"something#yahoo.com"
);
name = "Some Name";
},
{
emails = (
"john.public#gmail.com",
"john#public.name"
);
name = "John Q. Public";
},
[etc.]
I want to use a predicate to search these dictionaries by email address, returning any and all entries that have at least one email address that matches the search term.
So far, I have tried the method described in this question, just using CONTAINS, like so:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"emails CONTAINS[c] %#", searchString];
but any search just returned an empty array. If I search the name field instead, like so, it works fine:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"name CONTAINS[c] %#", searchString];
So I'm pretty sure it's specifically with searching the array. Ideas?
Aaand I figured it out. This works:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY emails CONTAINS[c] %#",currentString];
Thanks for being my rubber ducky, SO.
Related
i need to imlement search with to different class ons is Song and the other one is Album. etch one of them is represent song/album with his property (name,url,etc..).
now i am using this code to search song from my songArray by searching the name property, i need to also be able to search in the albumArray, also in the name property.
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.filteredSongArray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF._songName contains[cd] %#",searchText];
filteredSongArray = [NSMutableArray arrayWithArray:[_myDataMgr.songArray filteredArrayUsingPredicate:predicate]];
}
so my qustion is how can i tell the the predicate to look also in the AlbumArray
thank you all.
UPDATE:
i ran another predicate on the AlbumArray and after i got the results i added the two arrays. thank your replay was very helpful!
You can filter the array contents like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(songName CONTAINS[cd] %#)", searchText];
[self.filteredSongArray addObjectsFromArray [_myDataMgr.songArray filteredArrayUsingPredicate:predicate]];
No need to add SELF in predicate....
Hope this helps...!!!
I have written NSPredicate to filter the elements .
-(void)filterResult:(NSMutableArray*)array
{
search=TRUE;
filteredCategoryList=[[NSArray alloc]init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"roles beginsWith %# || UserResponseDetails IN %# ",array,array];
filteredCategoryList = [MembersList filteredArrayUsingPredicate:predicate];
[filteredCategoryList retain];
}
The Problem is I am getting one of the string like "sample;Example" in Roles and others are single string. How to write the Predicate condition to get the Element for Sample,Example,sample and Example.
.
I got a solution that I have to use 'contains' keyword instead of 'beginswith' in the predicate.
In core data I have an entity called keyword with a property 'text'. I want to find all keyword objects that are contained in a particular sting.
I tried:
[NSPredicate predicateWithFormat:#"%# contains[c] text", myString];
But that crashes. It seems like if it will only work if it's reversed.
The predicate works on the entity, so you have your predicate reversed. Since your entity's text property contains a single word, you'd split the string into multiple words and then test:
// This is very simple and only meant to illustrate the property, you should
// use a proper regex to divide your string and you should probably then fold
// and denormalize the components, etc.
NSSet* wordsInString = [NSSet setWithArray:[myString componentsSeparatedByString:#" "]];
NSPredicate* pred = [NSPredicate predicateWithFormat:#"SELF.text IN %#", wordsInString];
I think you're doing in reverse direction.
Try this.
[NSPredicate predicateWithFormat:#"text contains[cd] %#", myString];
Hey,
I have an Array of NSDictionaries (e.g. [tempArray addObject:[[[NSMutableDictionary alloc] initWithObjects:someArray forKeys:keys] autorelease]]; ) and when I try to search through it with NSPredicate it keeps giving me 'No Results'. (TempArray becomes self.patientList)
I have no compile-time warnings or errors, and if I NSLog([self.filteredArray count]); then it returns 0 as well.
Here is my relevant code:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(first contains[cd] %#)", self.searchBar.text];
self.filteredArray = [self.patientList filteredArrayUsingPredicate:predicate];
return [self.filteredArray count];
self.patientList and self.filteredArray are types NSMutableArray and NSArray respectively (changing self.filteredArray to NSMutableArray doesn't help).
I have tried using == instead of contains as well. Using #"SELF contains[cd] %#" only returns an item when the full item is typed (i.e. if the key 'name' is #"Bob" then if I type in Bob it will display it, but not when I type Bo or ob).
I'm really stumped on this one.
Thanks in advance!
Apparently first is a reserved word when using predicates. From Apple's documentation:
The following words are reserved:
AND, OR, IN, NOT, ALL, ANY, SOME,
NONE, LIKE, CASEINSENSITIVE, CI,
MATCHES, CONTAINS, BEGINSWITH,
ENDSWITH, BETWEEN, NULL, NIL, SELF,
TRUE, YES, FALSE, NO, FIRST, LAST,
SIZE, ANYKEY, SUBQUERY, CAST,
TRUEPREDICATE, FALSEPREDICATE
Just change your key to something else, firstName perhaps, and it will work.
You can use "like" instead:
first like[c] *%#*
At first glance, it seems correct. So then: do your dictionaries actually have a key called first? And if they do, does it match the case exactly?
I have two entities: Department and DepartmentInfo. Every Department has one or many DepartmentInfo Objects. Inside DepartmentInfo, there is an departmentName attribute.
I want to fetch all those Department objects which have a specific departmentName. So I make an NSFetchRequest for the Department entity, and use this fetch request:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SUBQUERY(departmentName, $s, $s.departmentName LIKE[c] %#).#count > 0", #"Marketing"];
It works, BUT: The LIKE[c] doesn't! I must match against the exact department name. If I do this, I will get no match:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SUBQUERY(departmentName, $s, $s.departmentName LIKE[c] %#).#count > 0", #"Mar"];
What could be wrong here?
Since Jason Coco didn't post this as answer, I do it:
Use #"Mar*" and you will match
The use of SUBQUERY here is unnecessary. You can achieve the same results using:
ANY departmentInfo.departmentName LIKE[c] 'Mar*'
Execute that against an array of Department objects and it'll work.