IOS之谓词的用法

来源:互联网 发布:手机版缩水软件 编辑:程序博客网 时间:2024/05/20 15:40
谓词就是一个判断的类

新建一个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);
0 0
原创粉丝点击