I am trying to write a predicate similar to
[NSPredicate predicateWithFormat:#"ANY manyNames in %#", allNames];
but instead of 'in' I need 'like'
[NSPredicate predicateWithFormat:#"ANY manyNames like %#", allNames];
This causes an error.
So I want to get results with some objects of manyNames, which are similiar to at least one of allNames.
I solved my problem by joining the strings and making one string of them, then looking for that.
Related
I have 3 core data entities.
MenuItems, Items, ItemLangs.
All entities has iItemId attribute. The user need to search using sItemName which can be found in the ItemLangs.
The UITableView contains array of MenuItems.
I have tried filteredArrayUsingPredicate but my predicate is not right. Do I need to loop through the array of MenuItems first before I can filter the array?
Here is my predicate:
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"sItemName LIKE %#", searchText];
May I know how can I use this to filter the array of MenuItems that I have?
Thank you!
What you need is a predicate like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"sItemName CONTAINS[cd] %#", searchText];
[cd] stands for case insensitiveness.
Alternately, if you like "LIKE", refer to this answer.
UPDATE:
You should also look at the data item you want to fetch - sItemName in your case. Through debugger, inspect what object does your data source array return? I am referring to the array you want to apply the above predicate to. Maybe there is some keypath hierarchy involved in getting through towards sItemName, in which case you need to use dot notation, such as Items.ItemLangs.sItemName.
Refer following links, it may help you:
1) How to implement SearchBar and search display controller in tableview in xib
2) first attempt at synamic filtering a search array
3) http://www.appcoda.com/search-bar-tutorial-ios7/
4) http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view
I am trying to write a query that gets a row when the id is equal to 1 (at the moment).
NSPredicate *predicate = [NSPredicate predicateWithFormat:[apt[#"id"] isEqualToNumber:1]];
So where id (from another database) is equal to 1. Now this is complaining all sorts of things but its saying it needs to be a BOOL. Is NSPredicate needing a BOOL?
Not sure I understand, or is there another way I can query? Can I use T-SQL in Mobile Services SDK Azure for iOS?
The predicate with format expects a string, you are sending in a bool. [apt[#"id"] isEqualToNumber:1] returns a bool.
Use the predicate in this manner (I have little experience with formatting predicates, so this will likely not actually work for you to copy and paste in).
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"id == 1"];
This will work if your objects in the array you are filtering have a property called id.
Something I understand a littler better is predicates with blocks. This could work for you too (I'm not entirely sure of your data structure, so you are gong to have to modify to suit your needs.
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id object, NSDictionary *bindings) {
return [object[#"id"] isEqualToNumber:1]; // Return YES for each object you want in filteredArray.
}]
I want to fetch entity by name matching the string \\xxxx\\yyyy\\zzzz from core data so i have used NSPredicate like NSPredicate *myPredicate = [NSPredicate predicateWithFormat:" name LIKE %#", \\xxxx\\yyyy\\zzzz]; But returns null for some times.I doubt that issue with backslashes so please help me any one.Is any way to fetch this entity from core data?
"LIKE" in a predicate does a simple wildcard matching (with ? and *).
As a consequence, the backslash character has a special meaning and has to be escaped
twice (once for the string literal and once for the "LIKE" operator):
[NSPredicate predicateWithFormat:"name LIKE %#", #"\\\\xxxx\\\\yyyy\\\\zzzz"]
But if you don't need the wildcard matching then use "==" (or "BEGINSWITH", "CONTAINS") instead:
[NSPredicate predicateWithFormat:"name == %#", #"\\xxxx\\yyyy\\zzzz"]
I've had a hard time putting together the correct search terms to find this answer, so I thought I'd try to ask it. I'm still getting to know Core Data.
I have a Player entity and a School entity. The player has a to-many relationship with schools.
I want all Player objects where the School name attribute can be one of several values (ie, I want all players that have gone to either Stanford, Yale, or Duke). I imagine that I need to use the 'IN' clause, something like
[NSPredicate predicateWithFormat:#"schools.name IN ("%#", "%#", "%#"), #"Stanford", #"Yale", #"Duke"];
Don't quote inside the predicate format string
[NSPredicate predicateWithFormat:#"ANY schools.name IN (%#, %#, %#)", #"Stanford", #"Yale", #"Duke"];
but this is a useless bit of code. Given an NSArray of names then I believe you can do this:
[NSPredicate predicateWithFormat:#"ANY schools.name IN %#", arrayOfNames];
Finally got it.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY schools IN %#", schoolObjectArray];
The relationship from Player to Schools is schools, and the schoolObjectArray contains the School NSManagedObjects that I want.
maybe?
[NSPredicate predicateWithFormat:#"schools.name IN (\"%#\", \"%#\", \"%#\")", #"Stanford", #"Yale", #"Duke"];
In my project i have a set of attributes in an entity.One of them is path as string.I want all the records which has my string as a subpath in the path.
Example:
Path:/var/mobile/Applications/C4ECC129-36D2-4428-8BB0- 6B1F4C971FC1/Library/Caches/Packages/1000
Mystring : Library/Caches/Packages/1000
I tried using Like and contains as below but failed.
[NSPredicate predicateWithFormat:#"bookPath like[c] '%%%#'",Mystring];
[NSPredicate predicateWithFormat:#"bookPath Contains[cd] '%#'",Mystring];
Can some one help in writing the predicate to fetch those records which contains mystring.
Really helps me a lot.
Tnx in advance
You need to have a predicate like this
[fecthRequest setPredicate:[NSPredicate predicateWithFormat:#"bookPath endswith[cd] %#", myString]];
or this
[fecthRequest setPredicate:[NSPredicate predicateWithFormat:#"bookPath contains[cd] %#", myString]];
The no results is due to single quotes around your %#. From the documentation (Dynamic Property Names):
string variables are surrounded by quotation marks when they are
substituted into a format string using %#
About the predicates I really suggest to use the first, if the sub-path you are looking for it is always in the final part of the original path.
About using predicates, I really suggest to read String Comparisons.
Hope that helps.
Try this:
[NSPredicate predicateWithFormat:#"%K contains %#", #"bookPath", Mystring];