OC-谓词(NSPredicate)

OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。

下面直接上代码操作一下.

Person.h

#import 

@interface Person : NSObject

@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) NSInteger age;

+ (instancetype)personWithName:(NSString *)name andAge:(NSInteger)age;
@end

创建一个Person类,里面有_name和_age两个属性.

Person.m

#import "Person.h"

@implementation Person

+ (instancetype)personWithName:(NSString *)name andAge:(NSInteger)age {
    
    Person *person = [[Person alloc]init];
    person.name = name;
    person.age = age;
    
    return person;
}


@end

main.h

#import 
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        NSArray *persons = [NSArray arrayWithObjects:
                            [Person personWithName:@"zhangsan" andAge:20],
                            [Person personWithName:@"lisi" andAge:25],
                            [Person personWithName:@"wangwu" andAge:22],
                            [Person personWithName:@"zhaoliu" andAge:30],
                            [Person personWithName:@"duqi" andAge:33],
                            [Person personWithName:@"zhengba" andAge:50],
                            [Person personWithName:@"liujiu" andAge:28],
                            [Person personWithName:@"zhangshi" andAge:79],
                            nil];
        

创建一个数组,元素是Person *对象.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d", 30];

NSArray *filterArray = [persons filteredArrayUsingPredicate:predicate];

创建一个谓词,也就是一个筛选条件,即年龄在30以下的,并且创建一个新的数组fileArray来接受符合条件的元素.

predicate = [NSPredicate predicateWithFormat:@"age < %d && name = %@", 30, @"zhangsan"];
filterArray = [persons filteredArrayUsingPredicate:predicate];
        
        for (Person *p in filterArray) {
            
            NSLog(@"pName : %@", p.name);
        }

当然也可以创建多个筛选条件的谓词

 predicate = [NSPredicate predicateWithFormat:@"self.name IN {'小白', '小黑'} || self.age IN {79, 28}"];

 filterArray = [persons filteredArrayUsingPredicate:predicate];
        for (Person *p in filterArray) {
            
            NSLog(@"pName : %@", p.name);
        }

使用IN符号

predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'A'"]; filterArray = [persons filteredArrayUsingPredicate:predicate];

        for (Person *p in filterArray) {

            NSLog(@"pName : %@", p.name);
        }

查找以字符''开头的

predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'A'"]; filterArray = [persons filteredArrayUsingPredicate:predicate];

        for (Person *p in filterArray) {

            NSLog(@"pName : %@", p.name);
        }

查找以字符''结尾的

predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
filterArray = [persons filteredArrayUsingPredicate:predicate];
        for (Person *p in filterArray) {
            
            NSLog(@"pName : %@", p.name);
        }

查找包含字符''的

//like 匹配任意多个字符  
//name中只要有s字符就满足条件  
predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];  
//?代表一个字符,下面的查询条件是:name中第二个字符是s的  
predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];  

谓词的使用很方便,和我们当初在操作数据库的时候很像,但是它对我们进行过滤操作提供了很大的便捷。

你可能感兴趣的:(OC-谓词(NSPredicate))