• NSPredicate 详解


    Cocoa框架中的NSPredicate用于查询,原理和用法类似于SQL中的where,作用相当于数据库的过滤器。

    下面是几种常用的方法。

    1.比较运算符>,<,==,>=,<=,!=
      可用于数值及字符串

       NSArray *firstNames = @[ @"", @"", @"", @"" ];

        NSArray *lastNames = @[ @"", @"", @"", @"" ];

        NSArray *ages = @[ @34, @27, @20, @31 ];

        NSMutableArray *people = [NSMutableArray array];

        [firstNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            Person *person = [[Person alloc] init];

            person.firstName = firstNames[idx];

            person.lastName = lastNames[idx];

            person.age = ages[idx];

            [people addObject:person];

        }];

         NSPredicate *liPredicate = [NSPredicate predicateWithFormat:@"firstName = ''"];

        NSPredicate *thirtiesPredicate = [NSPredicate predicateWithFormat:@"age >= 30"];

        NSArray  *personArray =  [people filteredArrayUsingPredicate:liPredicate];

            for (Person *people in personArray) {

            NSLog(@"  : %@ ,%@, %@", people.firstName,people.lastName,people.age);

        }

         NSArray  *ageArray =  [people filteredArrayUsingPredicate:thirtiesPredicate];

        for (Person *people in ageArray) {

            NSLog(@"age  : %@ ,%@, %@", people.firstName,people.lastName,people.age);

        }

     Log:

    2015-09-22 15:51:57.611 CoreDataDemo[7464:207110]   : ,, 30

    2015-09-22 15:51:57.611 CoreDataDemo[7464:207110]   : ,, 31

    2015-09-22 15:51:57.612 CoreDataDemo[7464:207110] age  : ,, 34

    2015-09-22 15:51:57.612 CoreDataDemo[7464:207110] age  : ,, 31

    2.范围运算符:IN、BETWEEN


    例:@"number BETWEEN {1,5}"
          @"address IN {'shanghai','beijing'}"

      IN:

        NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil];

        NSString *string = @"ang";

        NSPredicate *pred = [NSPredicate predicateWithFormat:@"%@ in SELF",string];

        NSLog(@"%@",[array filteredArrayUsingPredicate:pred]);

    Log:

    2015-09-22 16:02:51.360 CoreDataDemo[7765:215760] (

        shanghai,

        guangzou

    )

     

    bewteen:

       NSPredicate *  predicate = [NSPredicate predicateWithFormat:

                     @"age BETWEEN { 31, 34 }"];

         NSArray *results = [people filteredArrayUsingPredicate: predicate];

        for (Person *people in results) {

            NSLog(@"  : %@ ,%@, %@", people.firstName,people.lastName,people.age);

        }

    log:

    2015-09-22 16:11:14.634 CoreDataDemo[7984:222771]   : ,, 34

    2015-09-22 16:11:14.634 CoreDataDemo[7984:222771]   : ,, 31

     

    3.字符串本身:SELF 

    例:@“SELF == ‘APPLE’"


    4.字符串相关:BEGINSWITH、ENDSWITH、CONTAINS

    例:@"name CONTAIN[cd] 'ang'"   //包含某个字符串
           @"name BEGINSWITH[c] 'sh'"     //以某个字符串开头
           @"name ENDSWITH[d] 'ang'"      //以某个字符串结束
            注:[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。

    5.通配符:LIKE
    例:@"name LIKE[cd] '*er*'"    //*代表通配符,Like也接受[cd].
           @"name LIKE[cd] '???er*'"

     

  • 相关阅读:
    spring -项目功能介绍
    【HQL】分页查询 、对象导航查询、外置命名查询、连接查询、查询过滤器、统计查询
    【HQL】属性查询、条件查询
    【HQL】hibernate查询语言hql
    Hibernate- 表联系
    struts 配置过程 -一个计算器程序
    【DRP】-JSTL核心库 c:out标签
    .NET 使用sock5做代理(不是搭建服务端)
    重磅新闻!昨日阿里云发布首款云电脑“无影”,到底如何呢?
    C#如何实现获取电脑硬件相关的配置信息呢?
  • 原文地址:https://www.cnblogs.com/menchao/p/4829283.html
Copyright © 2020-2023  润新知