1、创建谓词
NSPredicate *predicate = [NSPredicate initWithFormat:@"name == 'XXX'"];
2、计算谓词
BOOL match = [predicate evalueWithObject:car];
3、过滤器
NSArray *result;
result = [cars filteredArrayUsingPredicate:predicate];
这里和之前的KVC不一样,因为这里得到的是一组对象,而KVC得到的是对应的值
4、格式
NSPredicate *predicate = [NSPredicate initWithFormat:@"engine.hoursepower > %d",150];
<pre name="code" class="objc">NSPredicate *predicate = [NSPredicate initWithFormat:@"name == %@",@"aaa"];
<pre name="code" class="objc">NSPredicate *predicate = [NSPredicate initWithFormat:@"%K == %@",@name,@"aaa"];
NSDictionary *varDict;
varDict = [NSDictionary dictionaryWithObjectsAndKeys:@"aaa",@"NAME",nil];
predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower > $POWER"];
predicate = [predicateTemplate predicateWithSubstitutionVariables:varDict];
这里,还支持AND.OR.NOT
NSArray *betweens = [NSArray arrayWithObjects:[NSNumber numberWithInt: 50],[NSNumber numberWithInt:200],nil];
predicate = [NSPredicate predicateWithFormate:@"engine.horsepower BETWEEN %@",betweens];
pridicate = [NSPredicate predicateWithFormat:@"name IN{'AAA','BBB','CCC'}"];
5、SELF
NSArray *name1 = [NSArray arrayWithObjects:@"AAA",@"BBB",@"ccc",nil];
NSArray *name2 = [NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",nil];
predicate = [NSPredicate predicateWithFormate:@"SELF IN %@",name1];
results = [names2 filteredArrayUsingPredicate:predicate];
6、字符串运算符
BEGINSWITH,检查某个字符串是否以另一个字符串开头。
ENDSWITH,检查某个字符串是否以另一个字符串结尾。
CONTAINS,检查字符串中是否在另一个字符串内部
7、LIKE运算符
通配符所有为*
单个为?