码迷,mamicode.com
首页 > 其他好文 > 详细

NSPredicate 详解

时间:2015-09-22 18:32:14      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

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

 

 

 

NSPredicate 详解

标签:

原文地址:http://www.cnblogs.com/menchao/p/4829283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!