objective-c 谓词

基本概念

  cocoa中提供NSPredicate类,指定过滤器的条件。将符合条件的对象保留下来。

 

创建谓词

示例代码:

//创建谓词,设置一个条件
        //判断是否满足条件
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age > 20"];
        for (Person *per in arr) {
            //表示指定对象是否满足谓词条件
            BOOL ret = [predicate evaluateWithObject:per];
            if (ret) {
                NSLog(@"%@",per);
            }
        }


            //对数组过滤,返回一个符合谓词条件的数组
       NSArray *newarr = [arr filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",newarr);

运算符

  谓词不区分&& and || or

示例代码:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@“age>20 and age<22"];

IN

  查询要加''

示例代码:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@“name in {‘夜夜-7’,’夜夜-9’}“];

//

NSArray *namearr = @[@"夜夜-7",@"夜夜-8"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in %@",namearr];

关键字查询  

  BEGINSWITH  查找首字符符合条件的

  ENDSWITH  查找结尾字符符合条件的

  CONTAINS  查找包含字符的条件

  LIKE  模糊查找

示例代码:

NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"name BEGINSWITH ‘夜'"];

NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"name ENDSWITH ‘-5'"];

NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"name CONTAINS  ‘春虎'"];

NSPredicate *predicate5 = [NSPredicate predicateWithFormat:@"name LIKE '夜*'"];

谓词示例代码:

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property(nonatomic,copy)NSString *name;
@property(nonatomic,retain)NSNumber *age;


@end

Person.m

#import "Person.h"

@implementation Person

-(NSString *)description{
    NSString *str = [NSString stringWithFormat:@"name = %@,age = %@",_name,_age];
    return str;
}

@end

main.m

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSLog(@"Hello, World!");
        
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        for (int i = 1; i<10; i++) {
            Person *person = [[Person alloc] init];
            if (i<5) {
                person.name = [NSString stringWithFormat:@"春虎-%d",i];
            }else{
                person.name = [NSString stringWithFormat:@"夜夜-%d",i];
            }
            person.age = @(16+i);
            [arr addObject:person];
        }
        
        //创建谓词,设置一个条件
        //判断是否满足条件
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age > 20"];
        /*
         NSPredicate *predicate = [NSPredicate predicateWithFormat:@“name in {‘夜夜-7’,’夜夜-9’}“];
         
         或
         
         NSArray *namearr = @[@"夜夜-7",@"夜夜-8"];
         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in %@",namearr];
         */
        for (Person *per in arr) {
            //表示指定对象是否满足谓词条件
            BOOL ret = [predicate evaluateWithObject:per];
            if (ret) {
                NSLog(@"%@",per);
            }
        }
        
        
        //对数组过滤,返回一个符合谓词条件的数组
        NSArray *newarr = [arr filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",newarr);
        
        
        
        //BEGINSWITH 首字符
        NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"name BEGINSWITH '夜'"];
        for (Person *per in arr) {
            //表示指定对象是否满足谓词条件
            BOOL ret = [predicate2 evaluateWithObject:per];
            if (ret) {
                NSLog(@"BEGINSWITH:%@",per);
            }
        }
        
        //ENDSWITH 结尾字符
        NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"name ENDSWITH '-5'"];
        for (Person *per in arr) {
            //表示指定对象是否满足谓词条件
            BOOL ret = [predicate3 evaluateWithObject:per];
            if (ret) {
                NSLog(@"ENDSWITH:%@",per);
            }
        }
        
        //CONTAINS 是否包含了
        NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"name CONTAINS  '春虎'"];
        for (Person *per in arr) {
            //表示指定对象是否满足谓词条件
            BOOL ret = [predicate4 evaluateWithObject:per];
            if (ret) {
                NSLog(@"CONTAINS:%@",per);
            }
        }

        //LIKE 模糊查找
        NSPredicate *predicate5 = [NSPredicate predicateWithFormat:@"name LIKE '夜*'"];
        for (Person *per in arr) {
            //表示指定对象是否满足谓词条件
            BOOL ret = [predicate5 evaluateWithObject:per];
            if (ret) {
                NSLog(@"like:%@",per);
            }
        }
    }
    return 0;
}

运行结果:

2014-01-03 11:10:49.852 谓词[1595:303] Hello, World!
2014-01-03 11:10:49.854 谓词[1595:303] name = 夜夜-5,age = 21
2014-01-03 11:10:49.854 谓词[1595:303] name = 夜夜-6,age = 22
2014-01-03 11:10:49.854 谓词[1595:303] name = 夜夜-7,age = 23
2014-01-03 11:10:49.854 谓词[1595:303] name = 夜夜-8,age = 24
2014-01-03 11:10:49.855 谓词[1595:303] name = 夜夜-9,age = 25
2014-01-03 11:10:49.855 谓词[1595:303] (
    "name = U591cU591c-5,age = 21",
    "name = U591cU591c-6,age = 22",
    "name = U591cU591c-7,age = 23",
    "name = U591cU591c-8,age = 24",
    "name = U591cU591c-9,age = 25"
)
2014-01-03 11:10:49.855 谓词[1595:303] BEGINSWITH:name = 夜夜-5,age = 21
2014-01-03 11:10:49.855 谓词[1595:303] BEGINSWITH:name = 夜夜-6,age = 22
2014-01-03 11:10:49.856 谓词[1595:303] BEGINSWITH:name = 夜夜-7,age = 23
2014-01-03 11:10:49.856 谓词[1595:303] BEGINSWITH:name = 夜夜-8,age = 24
2014-01-03 11:10:49.856 谓词[1595:303] BEGINSWITH:name = 夜夜-9,age = 25
2014-01-03 11:10:49.857 谓词[1595:303] ENDSWITH:name = 夜夜-5,age = 21
2014-01-03 11:10:49.857 谓词[1595:303] CONTAINS:name = 春虎-1,age = 17
2014-01-03 11:10:49.857 谓词[1595:303] CONTAINS:name = 春虎-2,age = 18
2014-01-03 11:10:49.857 谓词[1595:303] CONTAINS:name = 春虎-3,age = 19
2014-01-03 11:10:49.858 谓词[1595:303] CONTAINS:name = 春虎-4,age = 20
2014-01-03 11:10:49.860 谓词[1595:303] like:name = 夜夜-5,age = 21
2014-01-03 11:10:49.860 谓词[1595:303] like:name = 夜夜-6,age = 22
2014-01-03 11:10:49.861 谓词[1595:303] like:name = 夜夜-7,age = 23
2014-01-03 11:10:49.861 谓词[1595:303] like:name = 夜夜-8,age = 24
2014-01-03 11:10:49.861 谓词[1595:303] like:name = 夜夜-9,age = 25
Program ended with exit code: 0
原文地址:https://www.cnblogs.com/mo-shou/p/3503322.html