首页 > 代码库 > NSPredicate的在查询中的简单使用
NSPredicate的在查询中的简单使用
上代码吧,每块代码都可以直接跑,并附有注释和运行结果
//直接使用 == 严格匹配 NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"c", @"cd", @"cdd", @"cd", 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", @"cd", @"cdd", @"dd",@"dcd",@"cdc", nil]; NSString *match = @"c*";//以c开头 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match]; NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);
打印结果:
result===(
c1,
cd,
cdd,
cdc
)
//忽略大小写进行匹配 NSArray *arrOriginal = [NSArray arrayWithObjects:@"Cdd",@"cDc",@"cdd",@"dcdd", nil]; NSString *match = @"cd*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[c] %@", match]; NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);
打印结果:
result===(
Cdd,
cDc,
cdd
)
//利用正则表达是进行过滤 NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"C3",@"d2",@"c2", nil]; NSString *match = @"^c\\d?"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match]; NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);
打印结果:
result===(
c1,
c2
)
//利用过滤器(数组)进行过滤 NSArray *arrFilter = [NSArray arrayWithObjects:@"aa", @"bb", nil]; NSArray *arrOriginal = [NSArray arrayWithObjects:@"dds",@"bb",@"c1",@"C3",@"bb",@"aa",@"c2", nil]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrFilter];//不包含符合过滤器的内容 NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);
打印结果:
result===(
dds,
c1,
C3,
c2
)
NSPredicate的在查询中的简单使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。