NSPredicate 谓词
cocoa 中提供了 NSPredicate 类,指定过滤器的条件。将符合条件的对象保留下来。
正则表达式
// 待匹配字符串
NSString *string = @"201709----201710";
// --------------------------------
// 1⃣️ NSPredicate
// 正则表达式
NSString *regex = @"^\\d{6}\\D+\\d{6}$";
// 创建 NSPredicate 实例对象
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
// 是否匹配
BOOL isMatch = [predicate evaluateWithObject:string];
NSLog(@"%@",isMatch ? @"匹配" : @"不匹配");
// --------------------------------
// 2⃣️ NSString
// 返回匹配到的第一个字符串范围
NSRange range = [string rangeOfString:@"\\d+" options:NSRegularExpressionSearch];
if (range.location != NSNotFound) {
NSLog(@"%@",[string substringWithRange:range]); // 201709
}
// --------------------------------
// 3⃣️ NSRegularExpression
// 创建 NSRegularExpression 实例对象
NSRegularExpression *regularExpression =
[NSRegularExpression regularExpressionWithPattern:regex
options:NSRegularExpressionCaseInsensitive
error:nil];
// 1.查找第一个匹配结果,如果查找不到则返回 nil
NSTextCheckingResult *firstMatch =
[regularExpression firstMatchInString:string
options:NSMatchingReportCompletion
range:NSMakeRange(0, string.length)];
if (firstMatch) {
// 如果匹配到结果,就打印
NSLog(@"%@",[string substringWithRange:firstMatch.range]);
}
// 2.查找字符串中所有的匹配结果
NSArray *matches = [regularExpression matchesInString:string
options:NSMatchingReportProgress
range:NSMakeRange(0, string.length)];
if (matches.count > 0) {
[matches enumerateObjectsUsingBlock:^(NSTextCheckingResult *obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"第 %lu 个结果 = %@",idx, [string substringWithRange:obj.range]);
}];
}
创建谓词
// 设置谓词条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age <= 28"];
for (Person *person in array) {
// 表示指定的对象是否满足谓词条件
if ([predicate evaluateWithObject:person]) {
//NSLog(@"person name : %@", person.name);
}
}
// 返回一个符合谓词条件的数组
NSArray *newArray = [array filteredArrayUsingPredicate:predicate];
for (Person *person in newArray) {
NSLog(@"person name : %@", [person valueForKey:@"_name"]);
}
格式占位符
// 格式占位符号
NSPredicate *pre = [NSPredicate predicateWithFormat:@" age <= %d", 30];
NSArray *array2 = [array filteredArrayUsingPredicate:pre];
for (Person *person in array2) {
NSLog(@"person name 2 %@", [person valueForKey:@"_name"]);
}
运算符
- 运算符号 的加⼊入 谓词不区分⼤大⼩小 && AND || OR
NSPredicate *pre3 = [NSPredicate predicateWithFormat:@"name > 'bruse' &&
age < %d", 30];
NSArray *array4 = [array filteredArrayUsingPredicate:pre3];
// 关键字 注意字符串⼀一定要添加''
NSPredicate *pre4 = [NSPredicate predicateWithFormat:@"self.name IN
{'rose', 'bruse'}"];
NSArray *array5 = [array filteredArrayUsingPredicate:pre4];
NSLog(@"person name : %@", [array5 valueForKey:@"_name"]);
关键字
- 以 ** 开始 ——BEGINSWITH
// BEGINSWITH 检查某个字是否以**开头
NSPredicate *pre5 = [NSPredicate predicateWithFormat:@"self.name
BEGINSWITH 'J'"];
NSArray *array6 = [array filteredArrayUsingPredicate:pre5];
NSLog(@"person name : %@", [array6 valueForKey:@"name"]);
- 以 ** 结束 ——ENDSWITH
// ENDSWITH 检查某个字符是以**结尾
NSPredicate *pre6 = [NSPredicate predicateWithFormat:@"self.name endswith
'e'"];
NSArray *array7 = [array filteredArrayUsingPredicate:pre6];
NSLog(@"array7 : %@", [array7 valueForKey:@"name"]);
- 包含 ——CONTAINS
// CONTAINS 检查包含某个字符
NSPredicate *pre8 = [NSPredicate predicateWithFormat:@"self.name CONTAINS
'⼩小'"];
NSArray *array8 = [array filteredArrayUsingPredicate:pre8];
NSLog(@"array8 : %@", [array8 valueForKey:@"name"]);