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; } )