zoukankan      html  css  js  c++  java
  • 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*'"

     

  • 相关阅读:
    redis 使用
    VS----id为xxxx的进程当前未运行 问题
    bootstrap--------bootstrap table显示行号
    js--------js获取当前时间,返回日期yyyy-MM-dd
    CLR via C#--------CLR的执行模式
    Python链表成对调换
    Python去除列表中的重复元素
    MySQL索引背后的数据结构及算法原理
    Python 垃圾回收机制
    Python 里的拷贝
  • 原文地址:https://www.cnblogs.com/menchao/p/4829283.html
Copyright © 2011-2022 走看看