//predicate謂詞:常用的是過濾數(shù)組中的元素
//最常用的三種
//1.@"SELF CONTAINS 'aa'"包含某個字符串
//2.@"SELF BEGINSWITH 'aa'"以某個字符串開頭
//3.@"SELF ENDSWITH 'ng'"以某個字符串結(jié)尾
NSArray *array = @[@"fuzhou", @"xiamen", @"longyan", @"quanzhou", @"putian", @"jinjiang", @"sanming", @"zhanzhou", @"xianyou", @"siming", @"huli"];
//創(chuàng)建謂詞的NSPredicate對象查找某些符合要求的字符串
NSPredicate *containPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'an'"];
NSArray *containArray = [array filteredArrayUsingPredicate:containPredicate];
NSPredicate *beginPredicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH 'fu'"];
NSArray *beginArray = [array filteredArrayUsingPredicate:beginPredicate];
NSPredicate *endPredicate = [NSPredicate predicateWithFormat:@"SELF ENDSWITH 'ng'"];
NSArray *endArray = [array filteredArrayUsingPredicate:endPredicate];
//LIKE模糊查詢
//*代表任意位置
//?代表一位
//??li---->*li
NSPredicate *likePredicate = [NSPredicate predicateWithFormat:@"SELF LIKE '??li'"];
NSArray *likeArray = [array filteredArrayUsingPredicate:likePredicate];
//查找某個元素字符串
NSString *str1 = @"xiamen";
//==固定兩個'=='
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@",str1];
NSArray *ary = [array filteredArrayUsingPredicate:predicate];
//查詢一個數(shù)組中是否包含另一個數(shù)組中的元素(兩個數(shù)組中是否有相同的內(nèi)容)
NSArray *array2 = @[@"xiamen", @"nanjing", @"zhengzhou"];
//SELF IN %@去重
NSPredicate *betweenPredicate = [NSPredicate predicateWithFormat:@"SELF IN %@",array2];
NSArray *betweenArray = [array filteredArrayUsingPredicate:betweenPredicate];
//利用謂詞判斷比較數(shù)值的大小
NSPredicate *numPredicate = [NSPredicate predicateWithFormat:@"self >= 10"];
BOOL isBig = [numPredicate evaluateWithObject:@8];
NSLog(@"%@",isBig ?@"大":@"小");
//前后綴篩選
BOOL has1 = [str1 hasPrefix:@"ba"]
BOOL has2 = [str1 hasSuffix:@"def"];
//謂詞的表達式
NSString *str =@"self beginswith [c] 'a'";
//開頭beginswith [c]不區(qū)分大小寫
//容量contains是否包含
//結(jié)尾endswith
//開始匹配
BOOL isOk = [predicate evaluateWithObject:@"Amy"];
//固定位?:一位模糊位*
@"self like '???b*'"
//判斷郵箱
@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}"
//根據(jù)謂詞過濾數(shù)組
arr = [arr filteredArrayUsingPredicate:pre];
//過濾數(shù)組相交部分
NSPredicate *pre = [NSPredicate predicateWithFormat:@"self in %@",arr1];
//條件篩選
@"self.age < 14"