Question

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!

Was it helpful?

Solution

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/

OTHER TIPS

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top