谓词就是一个判断的类
例
新建一个Model类
属性
@property (strong , nonatomic) NSString* name;
@property (assign , nonatomic) int num;
在ViewController的viewDidLoad中
Model* modelA = [[Model alloc] init];
//设置Model类里面的属性值
[modelA setValue:@"xiaonan" forKey:@"name"];
[modelA setValue:[NSNumber numberWithInt:20] forKey:@"num"];
//作用一
//创建谓词的对象 即判断条件对象predicate1
NSPredicate* predicate1 = [NSPredicate predicateWithFormat:@"name=='xiaonan'"];
NSPredicate* predicate2 = [NSPredicate predicateWithFormat:@"num ==20"];
//然后可以用predicate1条件对象和modelA比较
if ([predicate1 evaluateWithObject:modelA]) {
NSLog(@"zxc");
}
if ([predicate2 evaluateWithObject:modelA]) {
NSLog(@"mnbv");
}
//作用二
NSArray* array = @[@"af",@"bg"];
NSArray* array2 = @[@"af",@"fsd",@"bg",@"tre"];
NSPredicate* thePredicate = [NSPredicate predicateWithFormat:@"NOT(SELF in %@)",array];
NSArray* arr3 = [array2 filteredArrayUsingPredicate:thePredicate];
NSLog(@"%@",arr3);
例
新建一个Model类
属性
@property (strong , nonatomic) NSString* name;
@property (assign , nonatomic) int num;
在ViewController的viewDidLoad中
Model* modelA = [[Model alloc] init];
//设置Model类里面的属性值
[modelA setValue:@"xiaonan" forKey:@"name"];
[modelA setValue:[NSNumber numberWithInt:20] forKey:@"num"];
//作用一
//创建谓词的对象 即判断条件对象predicate1
NSPredicate* predicate1 = [NSPredicate predicateWithFormat:@"name=='xiaonan'"];
NSPredicate* predicate2 = [NSPredicate predicateWithFormat:@"num ==20"];
//然后可以用predicate1条件对象和modelA比较
if ([predicate1 evaluateWithObject:modelA]) {
NSLog(@"zxc");
}
if ([predicate2 evaluateWithObject:modelA]) {
NSLog(@"mnbv");
}
//作用二
NSArray* array = @[@"af",@"bg"];
NSArray* array2 = @[@"af",@"fsd",@"bg",@"tre"];
NSPredicate* thePredicate = [NSPredicate predicateWithFormat:@"NOT(SELF in %@)",array];
NSArray* arr3 = [array2 filteredArrayUsingPredicate:thePredicate];
NSLog(@"%@",arr3);