本文介绍了NSPredicate-带参数的动态谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是NSPredicates的新手,但我知道基本知识.
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.
希望您能理解.
谢谢!
推荐答案
使用以下
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
}
我也建议您阅读这篇文章 http://nshipster.com/nspredicate/
also I suggest to read this article http://nshipster.com/nspredicate/
这篇关于NSPredicate-带参数的动态谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!