NSPredicate
谓词条件过滤器,通常用于过滤数组数据,原理和用法都相似于SQL中的where,做用至关于数据库的过滤取。数据库
+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...;
NSarray
过滤//使用指定的谓词过滤NSArray集合,返回符合条件的元素组成的新集合 - (NSArray*)filteredArrayUsingPredicate:(NSPredicate *)predicate;
NSMutableArray
过滤//使用指定的谓词过滤NSMutableArray,剔除集合中不符合条件的元素 - (void)filterUsingPredicate:(NSPredicate *)predicate;
NSSet
过滤- (NSSet*)filteredSetUsingPredicate:(NSPredicate *)predicate;
NSMutableSet
过滤- (void)filterUsingPredicate:(NSPredicate *)predicate;
NSOrderedSet
过滤- (NSOrderedSet<ObjectType> *)filteredOrderedSetUsingPredicate:(NSPredicate *)p;
NSMutableOrderedSet
过滤- (void)filterUsingPredicate:(NSPredicate *)p;
建立模型类Person
,包含name
和age
两个属性数组
初始化数据源数组函数
Person *person1 = [Person initWithName:@"alex" andAge:22]; Person *person2 = [Person initWithName:@"ceciliaba" andAge:35]; Person *person3 = [Person initWithName:@"1" andAge:42]; Person *person4 = [Person initWithName:@"otto" andAge:18]; Person *person5 = [Person initWithName:@"yasha2" andAge:16]; NSArray *personArr = [NSArray arrayWithObjects:person1,person2,person3,person4,person5, nil];
定义谓词对象,设置过滤条件(过滤条件中,使用self.name和直接用name的效果同样)lua
//age小于30 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<30"]; //查询name=1的而且age大于40 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"]; //name以a开头的 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"]; //name以ba结尾的 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"]; //name为1/2/4,或者age在30-40之间的 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name IN {'1','2','4'} || age between{30,40}"]; //like 匹配任意多个字符 //name中只要有s字符就知足条件 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"]; //?表明一个字符,下面的查询条件是:name中第二个字符是s的 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
使用谓词条件过滤数组中的元素,过滤以后返回查询的结果code
NSArray *array = [personArr filteredArrayUsingPredicate:predicate];
在使用时,若是须要拼接属性名,其占位符为%K(注意大写)而不是%@,如:orm
NSString * key = @"age"; int age = 30; //拼接示例: [NSPredicate predicateWithFormat:@"%K < %d", key, age];
若是想动态改变判断的范围,能够使用$ 开头的占位符:对象
//用$AGE进行占位,能够动态修改$对应的值,这里的AGE能够是任意字符串 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age < $AGE"]; //修改AGE的值(AGE对应上面的$后的字符串),生成新的NSPredicate对象 NSPredicate *newPredicate = [predicate predicateWithSubstitutionVariables:@{@"AGE":@30}]; //使用newPredicate过滤数组 NSArray *array = [persons filteredArrayUsingPredicate: newPredicate];
附加符号:[c] [d] [cd] c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分ci
错误用法字符串
NSArray *array = [NSArray arrayWithObjects:@{@"city":@"beijing"},@{@"city":@"shanghai"},@{@"city":@"guangzhou"},@{@"city":@"wuhan"}, nil]; NSString *string = @"ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string]; NSMutableArray *tempArr = [NSMutableArray new]; for (NSDictionary *dic in array) { if ([pred evaluateWithObject:dic[@"city"]]) { [tempArr addObject:dic]; } } NSLog(@"tempArr = %@",tempArr); 输出: tempArr = ( { city = shanghai; }, { city = guangzhou; } )
这种用法虽然也能过滤出想要的数据,可是效率不高,后面的for循环其实能够省略的
正确用法string
NSPredicate *pred = [NSPredicate predicateWithFormat:@"city CONTAINS[cd] %@",string]; NSArray *resultArr = [array filteredArrayUsingPredicate:pred]; NSLog(@"resultArr = %@",resultArr); 输出: resultArr = ( { city = shanghai; }, { city = guangzhou; } )