NSPredicate - Dynamic predicate with arguments

ullstrm

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!

Avt

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/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章