美文网首页
谓词(过滤)

谓词(过滤)

作者: 杨继磊 | 来源:发表于2021-11-24 10:51 被阅读0次

    个人认为微词是个很不错的东西

    1.自定义对象

    @interface Person : NSObject
    
    @property(nonatomic , copy)NSString * name;
    
    @property(nonatomic , assign)int age;
    
    @property (nonatomic , retain)NSArray * familys;
    
    @end
    

    2.简单实用(数据比较)

    - (void)testObject1 {
        Person *person = [[Person alloc] init];
        person.name = @"gulong";
        person.age = 24;
       //整形比较
        NSPredicate * pred = [NSPredicate predicateWithFormat:@"age > 25"];
        BOOL match = [pred evaluateWithObject:person];
        NSLog(@"%s",(match)?"YES":"NO");
       //printf: NO
    }
    

    3.通配符和正则

    - (void)testObject2 {
        /* 
         BEGINSWITH:检查某个字符串是否以另一个字符串开头。
         ENDSWITH:检查某个字符串是否以另一个字符串结尾。
         CONTAINS:检查某个字符串是否以另一个字符串内部。
         [c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,又不区分发音符号。
         */
        Person *person = [[Person alloc] init];
        person.name = @"gulong";
        person.age = 24;
        
        /* 通配符 */
        NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"name LIKE[cd] '*er*'"];
             //*代表通配符Like还接受[cd].
        predicate1 = [NSPredicate predicateWithFormat:@"name LIKE[cd] '???er*'"];
        //?只匹配一个字符并且还可以接受[cd].
        //*表示有多个占位符
        /* 正则匹配 */
        NSString *regex = @"[A-Za-z]+";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        
        if ([predicate evaluateWithObject:@"huang"]) {
            NSLog(@"字符串是否为纯字母");
        }
        //printf 字符串是否为纯字母
    }
    

    4.数组的过滤1(里面的每个对象都是字符串)

    - (void)testArray1 {
        NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil];
        NSString *string = @"ang";
      //获取带有ang的所有字符串,然后形成一个数组
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string];
        NSLog(@"%@",[array filteredArrayUsingPredicate:pred]);
        //printf: shanghai,guangzou
    }
    

    5.数组的过滤2 (使用自定义的person类)

    - (void)testArray2 {
        Person *person1 = [[Person alloc] init];
        person1.name = @"long";
        person1.age = 23;
        
        Person *person2 = [[Person alloc] init];
        person2.name = @"gu";
        person2.age = 23;
        
        Person *person3 = [[Person alloc] init];
        person3.name = @"long";
        person3.age = 24;
    
        NSArray *persons = @[person1,person2,person3];
        //直接使用对象里面的属性就可以了,不能使用数组的某个对象名(其实format里面使用 “=” 和 “==”都是一样可行的)
         NSPredicate *pred = [NSPredicate predicateWithFormat:@"name == 'long'"]; //相等
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"name == %@",@"long"]; //相等
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"age == %d",23]; //相等
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"age = 23",]; //相等
        // (age > 23) AND (age < 26) AND 两者都要满足,OR 两种满足其一就可    
        // age between {23,26} 从23 到26之间 //@"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"; 
        //包含在其中用IN 
        NSArray *array = [persons filteredArrayUsingPredicate:pred]; 
        NSLog(@"array %@",[array description]); 
    }
    

    6.数组过滤3(对象里面还有数据)

    @property (nonatomic , retain)NSArray * familys;    //person.h
    
    - (void)testArray3 {
        Person *person1 = [[Person alloc] init];
        person1.name = @"huang";
        person1.age = 23;
        person1.familys = @[@"zhuge",@"zhangfei",@"liubei"];
        
        Person *person2 = [[Person alloc] init];
        person2.name = @"gu";
        person2.age = 23;
        person2.familys = @[@"suiquan",@"huanggai",@"lusun"];
        
        Person *person3 = [[Person alloc] init];
        person3.name = @"huang";
        person3.age = 24;
        person3.familys = @[@"caochao",@"xiahou",@"caozhi"];
        
        NSArray *persons = @[person1,person2,person3];
        /*
         * 数组包含时必须为 整个字符串,不是是部分(比如 huanggai 的 huang;这样是无数据的)
         */
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"familys CONTAINS 'huanggai'"];
        /*
         * 字符串包含时必须为 整个字符串,不是是部分(比如 huanggai 的 huang;是有用数据的)
         */
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"name CONTAINS 'huanggai'"];
        NSArray *array = [persons filteredArrayUsingPredicate:pred]; NSLog(@"array %@",[array description]); //打印 person2 
    }
    

    相关文章

      网友评论

          本文标题:谓词(过滤)

          本文链接:https://www.haomeiwen.com/subject/oldntrtx.html