zoukankan      html  css  js  c++  java
  • 谓词的使用

    谓词就是一个判断的类

    新建一个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);

  • 相关阅读:
    scala-class
    uva-10422-骑士-搜索题
    HDU 5724
    HDU 5728
    CodeForces 414B
    CodeForces 698A
    Codeforces Round #363 (Div. 2)
    BestCoder 2nd Anniversary 1001 Oracle
    BestCoder 2nd Anniversary 1002 Arrange
    HDU 4798
  • 原文地址:https://www.cnblogs.com/kexiaozhu/p/4427867.html
Copyright © 2011-2022 走看看