原文鏈接地址:http://blog.csdn.net/st646889325/article/details/53501286
簡介
NSPredicate類主要用來指定過濾器的條件,該對象可以準確的描述所需條件,對每個對象通過謂詞進行篩選,判斷是否與條件相匹配。謂詞是指在計算機中表示計算真假值的函數。原理和用法都類似于SQL查詢中的where,作用相當于數據庫的過濾取。主要用于從集合中分揀出符合條件的對象或者數據模型,也可以用于字符串的正則匹配.
一般的, NSPredicate
的篩選過濾的條件可以是, 邏輯運算符號(> , < , =),范圍運算符(IN,BETWEEN
),字符的包含/匹配/模糊搜索(BEGINSWITH,ENDSWITH,CONTAINS,LIKE
), 正則表達式(MATCHES
)等.
NSPredicate用法
1.謂詞過濾篩選的過程
1.創建NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"過濾條件"];
2.判斷指定的對象是否滿足NSPredicate創建的過濾條件
[predicate evaluateWithObject:model];
3.將array通過創建的predicate進行過濾,并返回符合條件的數據
NSArray *resultArr = [arrayfilteredArrayUsingPredicate:predicate];
這些方法既適用于字符串這樣的簡單對象的過濾, 又適用于數據模型的過濾.
舉例:
對簡單的字符串數組進行包含過濾
NSArray*array=@[@"abc",@"adbzc",@"adboc",@"skenabc",@"aksalkjjbc2c",@"111abc",@"a22bc4",@"abc4444",@"asdfad",@"alkmbc",@"abcopjoj",@"abjoc",@"khujabc",@"abc9074"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", @"abc"];
NSArray*resultArr = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%@",resultArr);
謂詞過濾的篩選條件
1.邏輯運算符號(> , < , = , >= , <=)
還可以跟邏輯運算符一起使用的: &&, || ,AND, OR 謂詞不區分大小寫
NSPredicate*predicate= [NSPredicate predicateWithFormat:@"age >20"];
NSPredicate*predicate= [NSPredicate predicateWithFormat:@"height > 180 && age > 10"];
NSPredicate*predicate= [NSPredicate predicateWithFormat:@"height >180OR age >10"];
2.范圍運算符(IN,BETWEEN
)
NSPredicate*predicate= [NSPredicate predicateWithFormat:"age BETWEEN {1,5}"];
NSPredicate*predicate= [NSPredicate predicateWithFormat:@"name IN {'abc','def','123'}"];
3.字符串之開頭和結尾(BEGINSWITH,ENDSWITH
)
BEGINSWITH
:以**開頭
ENDSWITH
:以**結尾
//name以N打頭
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name BEGINSWITH 'N'"];
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name BEGINSWITH[c] 'M'"];
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name BEGINSWITH[d] 'C'"];
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] 'w'"];
注:字符串類的篩選條件中, [c]不區分大小寫[d]不區分發音符號即沒有重音符號[cd]既不區分大小寫,也不區分發音符號。
4.字符串之包含和模糊查詢(CONTAINS,LIKE
)
CONTAINS
:包含某個字符串
LIKE
:模糊查詢
NSPredicate*predicate= [NSPredicate predicateWithFormat:@"name CONTAINS'N'"];
NSPredicate*predicate= [NSPredicate predicateWithFormat:@"name LIKE '*N*'"];
NSPredicate*predicate= [NSPredicate predicateWithFormat:@"name LIKE[cd]'???er*"];
注:LIKE中的?表示一個任意字符, *表示通配符
5.字符串匹配查詢(SELF
)
以上說的都是對象中的屬性匹配的篩選條件,如果數組中都是字符串,即非屬性匹配查詢, 需要用到SELF
.
NSPredicate*predicate= [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd]%@", @"abc"];
6.正則表達式(MATCHES
)
NSPredicate
使用MATCHES
匹配正則表達式,正則表達式的寫法采用international components
for Unicode (ICU)的正則語法。
NSString*regex=@"^A.+e$";
//以A 開頭,以e 結尾的字符。
NSPredicate*pre= [NSPredicate predicateWithFormat:@"SELF MATCHES%@", regex];
舉例
例一
NSArray *array=[NSArray arrayWithObjects:person1,person2,person3,person4,...,nil];
//方法一:手動過濾出age小于20的person數組
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age < 20"];
for(Person *person in array){
if([predicate evaluateWithObject:person]){
//判斷指定的對象是否滿足
}
}
//方法二:直接獲取過濾出age小于20的person數組
NSArray *persons = [array filteredArrayUsingPredicate:predicate];//獲取所有age小于20的person
例二
將self.dataSourceArray模型數組中, 對 groupname 和 diseasename兩個分別進行篩選, 將篩選模型添加到self.resulArr中, 并去重.
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"groupname CONTAINS[cd] %@",searchString];
NSArray *nameArr = [self.dataSourceArray filteredArrayUsingPredicate:namePredicate];
NSPredicate *diseasePredicate = [NSPredicate predicateWithFormat:@"diseasemc CONTAINS[cd] %@",searchString];
NSArray *diseaseArr = [self.dataSourceArray filteredArrayUsingPredicate:diseasePredicate];
NSMutableSet *set = [NSMutableSet setWithArray:nameArr];
[set addObjectsFromArray:diseaseArr];
self.resultArr = [set sortedArrayUsingDescriptors:@[[[NSSortDescriptor alloc] initWithKey:@"groupname" ascending:YES]]];
總結和注意
1.NSPredicate既適用于屬性匹配,對數據模型或者dic進行過濾; 又適用于簡單字符串的非屬性匹配. 區別只是謂詞的條件用法不同.
2.在字符串篩選的謂詞條件中, [c]不區分大小寫[d]不區分發音符號即沒有重音符號[cd]既不區分大小寫,也不區分發音符號。
3.在例子中, filteredArrayUsingPredicate
語句是進行過濾的執行. 然后, 我們進入到NSPredicate類中, 可以發現, NSPredicateSupport
對NSArray,NSMutableArray,NSSet,NSMutableSet,NSOrderedSet,NSMutableOrderedSet
這些類都是適用的.
@interface NSArray<ObjectType> (NSPredicateSupport)
- (NSArray<ObjectType> *)filteredArrayUsingPredicate:(NSPredicate *)predicate; // evaluate a predicate against an array of objects and return a filtered array
@end
@interface NSMutableArray<ObjectType> (NSPredicateSupport)
- (void)filterUsingPredicate:(NSPredicate *)predicate; // evaluate a predicate against an array of objects and filter the mutable array directly
@end
@interface NSSet<ObjectType> (NSPredicateSupport)
- (NSSet<ObjectType> *)filteredSetUsingPredicate:(NSPredicate *)predicate NS_AVAILABLE(10_5, 3_0); // evaluate a predicate against a set of objects and return a filtered set
@end
@interface NSMutableSet<ObjectType> (NSPredicateSupport)
- (void)filterUsingPredicate:(NSPredicate *)predicate NS_AVAILABLE(10_5, 3_0); // evaluate a predicate against a set of objects and filter the mutable set directly
@end
@interface NSOrderedSet<ObjectType> (NSPredicateSupport)
- (NSOrderedSet<ObjectType> *)filteredOrderedSetUsingPredicate:(NSPredicate *)p NS_AVAILABLE(10_7, 5_0); // evaluate a predicate against an ordered set of objects and return a filtered ordered set
@end
@interface NSMutableOrderedSet<ObjectType> (NSPredicateSupport)
- (void)filterUsingPredicate:(NSPredicate *)p NS_AVAILABLE(10_7, 5_0); // evaluate a predicate against an ordered set of objects and filter the mutable ordered set directly
@end