熟悉NSPredicate
NSArray *array = [NSArray arrayWithObjects:@"beijing", @"shanghai", @"guangzhou", @"wuhan", nil];
//在数组里面筛选含有@"be"的字符串
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", @"an"];
NSArray *tempArray = [array filteredArrayUsingPredicate:predicate];
[tempArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"tempArray_obj = %@", obj);
}];
/***************************************************************/
//NSPredicate给我留下最深印象的是两个数组求交集的一个需求,如果按照一般写法,需要2个遍历,但NSArray提供了一个filterUsingPredicate的方法,用了NSPredicate,就可以不用遍历!
NSArray *array1 = [NSArray arrayWithObjects:@1,@2,@3,@5,@6,@7, nil];
NSArray *array2 = [NSArray arrayWithObjects:@4,@5,@6, nil];
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF in %@", array2];
NSArray *temp = [array1 filteredArrayUsingPredicate:predicate1];
[temp enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"temp_obj = %@", obj);
}];
NSPredicate的比较功能
/*
比较运算符>,<,==,>=,<=,!=
可用于数值及字符串
例:@"number > 100"
*/
NSArray *array = [NSArray arrayWithObjects:@1,@2,@3,@4,@5,@6, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF >= 4"];
NSArray *filterArray = [array filteredArrayUsingPredicate:predicate];
[filterArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"filterArray_obj = %@", obj);
}];
Predicate范围运算功能
/*
范围运算符:IN、BETWEEN
例:@"number BETWEEN {1,5}"
@"address IN {'shanghai','beijing'}"
*/
NSArray *array = [NSArray arrayWithObjects:@1,@2,@3,@4,@5,@2,@6, nil];
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF in {2,5}"];
//"in"是找到数组中2和5两个元素
//"BETWEEN"是找到数组中2到5之间的元素
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN {2,5}"];
NSArray *rangeArray = [array filteredArrayUsingPredicate:predicate];
[rangeArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"rangeArray_obj = %@", obj);
}];
与自身相比的功能
/*
字符串本身:SELF
例:@“SELF == ‘APPLE’"
*/
NSArray *placeArray = [NSArray arrayWithObjects:@"Shanghai",@"Hangzhou",@"Beijing",@"Macao",@"Taishan", nil];
NSPredicate *predicatre = [NSPredicate predicateWithFormat:@"SELF == 'Beijing'"];
NSArray *selfArray = [placeArray filteredArrayUsingPredicate:predicatre];
[selfArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"selfArray_obj = %@", obj);
}];
字符串相关:BEGINSWITH、ENDSWITH、CONTAINS
/*
字符串相关:BEGINSWITH、ENDSWITH、CONTAINS
例:@"SELF CONTAIN[cd] 'ang'" //包含某个字符串
@"SELF BEGINSWITH[c] 'sh'" //以某个字符串开头
@"SELF ENDSWITH[d] 'ang'" //以某个字符串结束
注:[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。
*/
NSArray *placeArray = [NSArray arrayWithObjects:@"Shanghai",@"Hangzhou",@"Beijing",@"Macao",@"Taishan", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] 'an'"];
NSArray *relateArray = [placeArray filteredArrayUsingPredicate:predicate];
[relateArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"relateArray_obj = %@", obj);
}];
Predicate 的通配
/*
通配符:LIKE
例:@"name LIKE[cd] '*er*'"
// *代表通配符,Like也接受[cd].
@"name LIKE[cd] '???er*'"
*/
NSArray *placeArray = [NSArray arrayWithObjects:@"Shanghai",@"Hangzhou",@"Beijing",@"Macao",@"Taishan", nil];
NSPredicate *pradicate = [NSPredicate predicateWithFormat:@"SELF LIKE '*an*'"];
NSArray *cardArray = [placeArray filteredArrayUsingPredicate:pradicate];
[cardArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"cardArray_obj = %@", obj);
}];