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*'"

     

  • 相关阅读:
    网页布局 选择符 选择符权重
    css基础
    新手入门html 表格 表单 超链接 图片
    新手入门html
    批量安装Windows系统
    无人值守批量安装服务器
    PXE实现无人值守批量安装服务器
    小白必看:零基础安装Linux系统(超级详细)
    项目实战:rsync+sersync实现数据实时同步
    rsync学习笔记
  • 原文地址:https://www.cnblogs.com/menchao/p/4829283.html
Copyright © 2011-2022 走看看