This condition works
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"ANY region.beacons.major = %d",rangedBeacon.major.intValue];
But this not
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"ANY region.beacons.minor = nil"];
beacons is a list inside region object. Major and minor have type NSNumber
You may try this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY region.beacons.minor = $minor"];
predicate = [predicate predicateWithSubstitutionVariables:
[NSDictionary dictionaryWithObject:[NSNull null] forKey: #"minor"]];
I found solution
Because of ANY and "=nil" can't be in the same condition (explanation is here Core Data, NSPredicate, ANY key.path == nil)? solution of this problem looks like:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"SUBQUERY(region.beacons, $x, $x.minor == nil)"];
Related
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];
i'm using a NSPredicate to filter an array in my searchbar. This is my code which works perfectly.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.name contains[cd] %#",searchText];
filteredArray = [NSMutableArray arrayWithArray:[storesArray filteredArrayUsingPredicate:predicate]];
The problem is i want to add a second condition as the name which is the address. How can i do this? I've tried this, but then none of the predicates are working.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.name contains[cd] %#",searchText];
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:#"SELF.address contains[cd] %#",searchText];
NSPredicate *fullPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:#[predicate, predicate1]];
filteredArray = [NSMutableArray arrayWithArray:[storesArray filteredArrayUsingPredicate:fullPredicate]];
You can use the OR comparison operator:
NSPredicate *predicate =
[NSPredicate predicateWithFormat:#"SELF.name contains[cd] %# OR SELF.address contains[cd] %#", searchText, searchText];
I am new to NSPredicates, but I know the basics.
This is how my predicate looks now:
[NSPredicate predicateWithFormat:#"name contains[c] %#", searchText];
What I want to do, is to be able to create a predicate like this (Pseudo code):
NSPredicate *myPredicate = [NSPredicate predicateWithBaseString:#"name contains[c] _ARGUMENT_"];
And then use it in, for example a loop (pseudo code):
for(NSString *searchString in self.allStrings)
{
NSPredicate *myNewPredicate = [myPredicate predicateWithArgument:searchString];
//Here I do the searchOperations with my new predicate
}
I want to do this so that I can have a few base predicates and then put parameters on them for searching. I do not want to create my predicate again.
Hopefully you understand.
Thank you!
Use following
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"name contains[c] $ARGUMENT"];
for(NSString *searchString in self.allStrings)
{
NSPredicate *myNewPredicate = [myPredicate predicateWithSubstitutionVariables:#{#"ARGUMENT" :searchString}];
//Here I do the searchOperations with my new predicate
}
also I suggest to read this article http://nshipster.com/nspredicate/
It seems as if i just found the answer. I can use predicateWithSubstitutionVariables like this:
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"name contains[c] $searchText"];
NSDictionary *sub = [NSDictionary dictionaryWithObject:searchText forKey:#"searchText"];
NSPredicate *filter = [resultPredicate predicateWithSubstitutionVariables:sub];
I have the DB that contains an
NSString *Number
NSSet *Values
I can easily get all the results based on the string Number with the following:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"Number LIKE[c] %#", numberValue];
NSArray *result = [DB MR_findAllWithPredicate:predicate inContext:_context];
If instead of Number I want to pass an Array of values, how can I find in db all the results that contain the given array (NSSet)?
NSPredicate *predicate = [NSPredicate predicateWithFormat: ????
NSArray *result = [DB MR_findAllWithPredicate:predicate inContext:_context];
UPDATE
Based on Igoris answer :
[NSPredicate predicateWithFormat:#“(Values.#count == %d) AND (SUBQUERY(Values, $x, $x IN %#).#count == %d)", allValues.count, allValues, allValues.count];
This seem to work but it doesn t return all the values in result.
Just use IN:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"Number IN[c] %#", numberValues];
Where numberValues is NSArray containing numbers you are looking for
UPDATE:
After data structure clarifications I came up with the following predicate to get objects:
[NSPredicate predicateWithFormat:#“(Values.#count == %d) AND (SUBQUERY(Values, $x, $x IN %#).#count == %d)", allValues.count, allValues, allValues.count];
How about ANY and NONE?
[NSPredicate predicateWithFormat:#"ANY number == %#", numberValues];
if I want to find the word "color" in the string "red is a color in my crayonbox", how to I structure it?
Thank you,
NSString *stringToSearchFor = #"whatever you want";
[NSPredicate predicateWithFormat:#"name CONTAINS[cd] %#", stringToSearchFor];
To search for red OR ferrari...
NSPredicate *redPred = [NSPredicate predicateWithFormat:#"name CONTAINS[cd] 'red'"];
NSPredicate *ferraiPred = [NSPredicate predicateWithFormat:#"name CONTAINS[cd] 'ferrari'"];
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubPredicates:[NSArray arrayWithObjects:redPred, ferrariPred, nil]];