参考文档1
参考文档2
NSPredicate
正则表达式的处理
NSString *aString = @"3www";
NSString *regex = @"[A-Za-z]+";
//MATCHES 主要用于正则匹配处理
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if (![predicate evaluateWithObject:aString]) {
NSLog(@"aString 不是以字母开头");
}
//移动号段正则表达式
NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
//联通号段正则表达式
NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
// 电信号段正则表达式
NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
if (isMatch1 || isMatch2 || isMatch3) {
NSLog(@"手机号码正确");
}else{
NSLog(@"手机号码有误");
}
字符串数组的处理
NSString *string = @"ng";
NSArray *array1 = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"naijing", nil];
NSArray *array2 = [[NSArray alloc]initWithObjects:@"nanjing",@"baoji", nil];
//SELF 指消息接收者,数组中的具体元素
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string]; //包括特定字符串
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",string]; //包括特定字符串,不区分大小写
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH %@",string]; //以特定字符串开头
NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"SELF ENDSWITH %@",string]; //以特定字会串结尾
NSPredicate *predicate5 = [NSPredicate predicateWithFormat:@"SELF LIKE '*ng*'"]; //中间包括特定字符串
NSPredicate *predicate6 = [NSPredicate predicateWithFormat:@"SELF LIKE '???ng*'"]; //特定字符串前三个字符
NSPredicate *predicate7 = [NSPredicate predicateWithFormat:@"SELF LIKE '*ng'"]; //以特定字符串结尾
NSPredicate *predicate8 = [NSPredicate predicateWithFormat:@"SELF == %@",@"beijing"]; //等于特定字符
NSPredicate *predicate9 = [NSPredicate predicateWithFormat:@"SELF IN %@",array2];
NSPredicate *predicate10 = [NSPredicate predicateWithFormat:@"SELF IN {'beijing','shanghai'}"];
NSPredicate *predicate11 = [NSPredicate predicateWithFormat:@"length > 7"];// [object length]
NSArray *resultArray = [array1 filteredArrayUsingPredicate:predicate11];
//快速遍历数组
[resultArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@",obj);
}];
包含对象类数组的处理
@class Test;
Test *test1 = [[Test alloc]init]; test1.name = @"老王"; test1.code = @1;
Test *test2 = [[Test alloc]init]; test2.name = @"隔壁"; test2.code = @2;
Test *test3 = [[Test alloc]init]; test3.name = @"好"; test3.code = @3;
NSArray *array = @[test1,test2,test3];
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"name.length >= %ld",2];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"code == %@ OR name == %@",@2,@"老王"];
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"code >= %@ AND name.length >= %ld",@2,2];
NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"name == %@",@"老王"];
NSPredicate *predicate5 = [NSPredicate predicateWithFormat:@"%K == %@",@"code",@2];
NSArray *resultArray = [array filteredArrayUsingPredicate:predicate5];
//快速遍历数组
[resultArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@",obj);
}];
网友评论