閑敲棋子落燈花
閑敲棋子落燈花
謂詞:
謂詞,用來描述或判定客體性質(zhì)、特征或者客體之間關(guān)系的詞項挑格。
Coco為我們提供了一個類NSPredicate類,該類主要用于指定過濾器的條件沾歪,該對象可以準(zhǔn)確的描述所需條件漂彤,對每個對象通過謂詞進(jìn)行篩選,判斷是否與條件相匹配灾搏。謂詞表示計算真值或假值的函數(shù)挫望。
OC中的謂詞操作是針對于數(shù)組類型的,與數(shù)據(jù)庫中的查詢操作類似狂窑,數(shù)據(jù)源就是我們要查詢的數(shù)組媳板,我們不需要編寫很多代碼就可以去操作數(shù)組,同時也起到過濾的作用(按條件篩選)蕾域,使用簡單的謂詞語句拷肌,就可以從數(shù)組中過濾出我們想要的數(shù)據(jù)到旦。非常方便旨巷。在Java中是沒有這種技術(shù)的,但是有開源的框架已經(jīng)實現(xiàn)了此功能添忘。在iOS中可以用謂詞與搜索欄 UISearchController結(jié)合使用采呐。
謂詞使用簡單舉例:
- 首先建立幾個person實例對象并放入一個數(shù)組中
Person *per0 = [Person personWithName:@"Note3" age:55];
Person *per1 = [Person personWithName:@"AA" age:20];
Person *per2 = [Person personWithName:@"DD" age:25];
Person *per3 = [Person personWithName:@"GG" age:92];
Person *per4 = [Person personWithName:@"KK" age:38];
Person *per5 = [Person personWithName:@"2324" age:138];
Person *per6 = [Person personWithName:@"詹姆斯" age:81];
Person *per7 = [Person personWithName:@"周琦" age:18];
Person *per8 = [Person personWithName:@"韋德" age:46];
Person *per9 = [Person personWithName:@"HUAWEI3x" age:67];
NSArray *persons = [NSArray arrayWithObjects:per0, per1, per2, per3, per4, per5, per6, per7, per8, per9, nil];
//定義謂詞對象,謂詞對象中包含了過濾條件(篩選的條件)
//條件1: 年齡大于180的
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age > 180"];
//使用謂詞條件過濾數(shù)組中的元素,過濾之后返回查詢的結(jié)果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
for (Person *per in array)
{
NSLog(@"條件1 age >180 --> 名字:%@, 年齡:%ld", per.name, per.age);
}
結(jié)果截圖
//條件2 年齡小于50并且名字為韋德的
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = '韋德' && age < 50"];
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
for (Person *per in array) {
NSLog(@"條件2 age < 50 && name = 韋德 --> %@, %ld", per.name, per.age);
}
結(jié)果截圖
//條件3 使用in (包含)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name IN {'D','AA', '韋德'} "];
//這句話意思找到在IN后面的這個數(shù)組中元素作為名字的人,完全匹配才行
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"滿足條件人數(shù) %ld 個", array.count);
for (Person *per in array) {
NSLog(@"條件3結(jié)果 %@, %ld", per.name, per.age);
}
條件3結(jié)果
//條件4 name以H開頭的 (beginswith)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name beginswith 'H' "];
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"滿足條件的人數(shù) %ld 個", array.count);
for (Person *per in array) {
NSLog(@"條件4結(jié)果 %@, %ld", per.name, per.age);
}
條件4結(jié)果
//條件5 name以e3結(jié)尾的 (endswith)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name endswith 'e3'"];
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"滿足條件人數(shù) %ld 個", array.count);
for (Person *per in array) {
NSLog(@"條件5結(jié)果 %@, %ld", per.name, per.age);
}
條件5結(jié)果
//條件6 name中包含32的(不管在名字中的位置)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains '32'"];
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"滿足條件 %ld 個", array.count);
for (Person *per in array) {
NSLog(@"條件6結(jié)果 %@, %ld", per.name, per.age);
}
條件6結(jié)果
//條件7 name中相應(yīng)位置有某些字符 ?表示一個字符 *表示0個或多個字符
//predicate = [NSPredicate predicateWithFormat:@"name like '*3?'"];
這個條件意思 : 第二個字符是3后面幾個字符都行
//predicate = [NSPredicate predicateWithFormat:@"name like '?a*'"];
這個條件意思是:倒數(shù)第二個字符是a a前面不管幾個字符都行
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like '*姆*'"];
這個條件意思是: 名字是三個字符 且第二個字符是姆的
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"%ld", array.count);
for (Person *per in array) {
NSLog(@"條件7結(jié)果 %@, %ld", per.name, per.age);
}
條件7結(jié)果
??????????????????