1、NSPredicate
它有3个子类,NSComparisonPredicate、NSCompoundPredicate、NSExpression。
创建谓词后,如果谓词中没有占位符参加,即可使用NSPredicate的evaluateWithObject方法计算谓词的结果,该结果是一个BOOL值,如果该谓词中有占位符参数,则需要经过如下两步来计算谓词的结果:
1)调用谓词的predicateWithSubstitutionVariables:方法为占位符参数设置参数值
2)执行谓词的evaluateWithObject:方法计算谓词的返回结果
实例:
#import <Foundation/Foundation.h>
#import "FKUser.h"
int main(int argc , char * argv[])
{
@autoreleasepool{
//①:创建谓词,要求name的以s开头
NSPredicate* pred = [NSPredicate predicateWithFormat:
@"name like 's*'"];
FKUser* user1 = [[FKUser alloc] initWithName:@"sun"
pass:@"123"];
//②:对user1对象使用谓词执行判断。
BOOL result1 = [pred evaluateWithObject:user1];
NSLog(@"user1的name是否以s开头:%d", result1);
FKUser* user2 = [[FKUser alloc] initWithName:@"bai"
pass:@"563"];
//③:对user1对象使用谓词执行判断。
BOOL result2 = [pred evaluateWithObject:user2];
NSLog(@"user2的name是否以s开头:%d", result2);
}
}
2、使用谓词对集合进行过滤
1)NSArray提供了如下方法使用谓词来过滤集合
- -(NSArray*)filteredArrayUsingPredicate:(NSPredicate*)predicate:使用指定的谓词过滤NSArray集合,返回该集合中符合谓词条件的元素组合成新集合
2)NSMutableArray提供了如下方法使用谓词来过滤集合
- -(void)filterUsingPredicate:(NSPredicate*)predicate:使用指定的谓词过滤NSMutableArray集合,剔除该集合中不符合条件的元素
#import <Foundation/Foundation.h>
#import "FKUser.h"
int main(int argc , char * argv[])
{
@autoreleasepool{
NSMutableArray* array = [NSMutableArray
arrayWithObjects: [NSNumber numberWithInt:50],
[NSNumber numberWithInt:50],
[NSNumber numberWithInt:42],
[NSNumber numberWithInt:20],
[NSNumber numberWithInt:64],
[NSNumber numberWithInt:56],nil];
//①:创建谓词,要求该对象自身的值大于50
NSPredicate* pred1 = [NSPredicate predicateWithFormat:
@"SELF > 50"];
//②:使用谓词执行过滤,过滤后只剩下值大于50的集合元素
[array filterUsingPredicate:pred1];
NSLog(@"值大于50的元素:%@" , array);
NSSet* set = [NSSet setWithObjects:
[[FKUser alloc] initWithName:@"孙悟空"
pass:@"343"],
[[FKUser alloc] initWithName:@"金角大王"
pass:@"231"],
[[FKUser alloc] initWithName:@"猪八戒"
pass:@"659"],
[[FKUser alloc] initWithName:@"太上老君"
pass:@"743"],
[[FKUser alloc] initWithName:@"银角大王"
pass:@"985"], nil];
// 创建谓词,要求该对象的name值中包含'大王'
NSPredicate* pred2 = [NSPredicate predicateWithFormat:
@"name CONTAINS '大王'"];
// 执行过滤,过滤后集合只剩下两个元素
NSSet* newSet = [set filteredSetUsingPredicate:pred2];
NSLog(@"%@" , newSet);
}
}
3)在谓词中使用占位符参数
在谓词表达式中使用变量,则可以考虑在谓词表达式中使用占位符参数,谓词表达式中支持一下两个占位符参数
- %K:该占位符用于动态传入属性名
- %@:该占位符用于动态设置属性值
如果在谓词表达式中使用了占位符参数,则需要在调用predicateWithFormat:方法时为个占位符传入参数值。还可以在谓词表达式中使用动态改变的属性值。
通过以下方法为这些占位符参数传入参数值
- 先调用NSPredicate的predicateWithSubstitutionVariables:方法为占位符参数设置参数值,该方法返回一个可用的NSPredicate对象
- 直接调用NSPredicate的evaluateWithObject:substitutionVariables:方法同时完成参数设置和结果计算
如下实例:
#import <Foundation/Foundation.h>
#import "FKUser.h"
int main(int argc , char * argv[])
{
@autoreleasepool{
NSSet* set = [NSSet setWithObjects:
[[FKUser alloc] initWithName:@"孙悟空"
pass:@"343"],
[[FKUser alloc] initWithName:@"金角大王"
pass:@"231"],
[[FKUser alloc] initWithName:@"猪八戒"
pass:@"659"],
[[FKUser alloc] initWithName:@"太上老君"
pass:@"743"],
[[FKUser alloc] initWithName:@"银角大王"
pass:@"598"], nil];
NSString* propPath = @"name";
NSString* value = @"大王";
// 创建谓词,该谓词中包含了2个占位符
//①:后面的2个变量用于为占位符设置参数值
NSPredicate* pred = [NSPredicate predicateWithFormat:
@"%K CONTAINS %@" , propPath , value];
//②:执行过滤,过滤后集合只剩下两个元素
NSSet* newSet = [set filteredSetUsingPredicate:pred];
NSLog(@"%@" , newSet);
// 创建谓词,该谓词表达式中使用%K占位符,该占位符使用pass代替
//③:要求被比较对象的pass包含$SUBSTR子串。
NSPredicate* predTemplate = [NSPredicate predicateWithFormat:
@"%K CONTAINS $SUBSTR" , @"pass"];
// 使用NDDictionary指定SUBSTR的值为'43'
NSPredicate* pred1 = [predTemplate
predicateWithSubstitutionVariables:
[NSDictionary dictionaryWithObjectsAndKeys:
@"43" , @"SUBSTR", nil]];
//④:执行过滤,过滤后集合只剩下两个元素
NSSet* newSet1 = [set filteredSetUsingPredicate:pred1];
NSLog(@"%@" , newSet1);
//⑤:使用NDDictionary指定SUBSTR的值为'59'
NSPredicate* pred2 = [predTemplate
predicateWithSubstitutionVariables:
[NSDictionary dictionaryWithObjectsAndKeys:
@"59" , @"SUBSTR", nil]];
//⑥:执行过滤,过滤后集合只剩下两个元素
NSSet* newSet2 = [set filteredSetUsingPredicate:pred2];
NSLog(@"%@" , newSet2);
}
}