NSPredicate的在查询中的简单使用

前端之家收集整理的这篇文章主要介绍了NSPredicate的在查询中的简单使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码吧,每块代码都可以直接跑,并附有注释和运行结果

//直接使用 == 严格匹配
NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"c",@"cd",@"cdd",nil];
NSString *match = @"cd";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@",match];
NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate];
NSLog(@"result===%@",results);

打印结果:
result===(
cd,
cd
)
//like匹配,类似sql中的用法
NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"d",@"dd",@"dcd",@"cdc",nil];
NSString *match = @"c*";//以c开头
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@",results);

打印结果:
result===(
c1,
cd,
cdd,
cdc
)
//忽略大小写进行匹配
NSArray *arrOriginal = [NSArray arrayWithObjects:@"Cdd",@"cDc",@"dcdd",nil];
NSString *match = @"cd*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[c] %@",results);

打印结果:
result===(
Cdd,
cDc,
cdd
)
//利用正则表达是进行过滤
NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"C3",@"d2",@"c2",nil];
NSString *match = @"^c\\d?";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@",
c2
)
//利用过滤器(数组)进行过滤
NSArray *arrFilter = [NSArray arrayWithObjects:@"aa",@"bb",nil];
NSArray *arrOriginal = [NSArray arrayWithObjects:@"dds",@"c1",@"aa",nil];
NSPredicate *predicate  = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)",arrFilter];//不包含符合过滤器的内容
NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate];
NSLog(@"result===%@",results);
打印结果: result===( dds,c1,C3,c2 ) 原文链接:https://www.f2er.com/regex/361043.html

猜你在找的正则表达式相关文章