NSPredicate 的相关用法

时间:2021-10-25 10:46:18

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    /**

     *NSPredicate  的相关用法

     */

    [self testPredicateDefinition];

    [self testPredicateComparation];

    [self testPredicateRange];

    [self testPredicateComparationToSelf];

    [self testPredicateRelateToNSString];

    [self testPredicateWildcard];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{


    [self.navigationController pushViewController:[[ThirdViewController alloc] init] animated:YES];


}


- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:YES];

//    self.tabBarController.tabBar.hidden = NO;




}



#pragma mark - 1. 熟悉Predicate

- (void)testPredicateDefinition{


    NSArray *array1 = [NSArray arrayWithObjects:@1,@2,@3,@5,@5,@6,@7, nil];

    NSArray *array2 = [NSArray arrayWithObjects:@4,@5, nil];

    

    //1.              ****    SELF  in     ****

    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF  in %@",array2];

    NSArray *temp1 = [array1 filteredArrayUsingPredicate:predicate1];

    // 表示筛选array1array2中的元素!YES!其中SELF指向filteredArrayUsingPredicate的调用者。

    [temp1 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSLog(@"temp1 = %@",obj);

    }];

    /*

     2016-06-02 14:54:29.854 3D Touch[5146:156676] temp1 = 5

     2016-06-02 14:54:29.854 3D Touch[5146:156676] temp1 = 5

     */

    

    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF  in %@",array1];

    NSArray *temp2 = [array2 filteredArrayUsingPredicate:predicate2];

    // 表示筛选array2array1中的元素!YES!其中SELF指向filteredArrayUsingPredicate的调用者。

    [temp2 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSLog(@"temp2 = %@",obj);

    }];

    /*

     2016-06-02 14:54:29.854 3D Touch[5146:156676] temp2 = 5

     */

}



#pragma mark - 2.测试Predicate的比较功能

- (void)testPredicateComparation{

    /*

     (1)比较运算符>,<,==,>=,<=,!=

     可用于数值及字符串

     例:@"number > 100"

     */

    NSArray *array = [NSArray arrayWithObjects:@1,@2,@3,@4,@5,@2,@6, nil];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF > 4"];


    /*

    NSArray *array = [NSArray arrayWithObjects:@"1",@"3",@"5",@"7",@"9",@"11",@"12", nil];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"number > 4"];//错误

     */

    NSArray *fliterArray = [array filteredArrayUsingPredicate:predicate];

    [fliterArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSLog(@"测试Predicate的比较功能 fliterArray = %@",obj);

    }];

}


#pragma mark - 3. Predicate范围运算功能

- (void)testPredicateRange{

    /*

     (2)范围运算符:INBETWEEN

     例:@"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,1}"];// 找到 in 的意思是array{1,2,5}的元素

//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN {2,5}"];// 找到 BETWEEN 的意思是array 2 - 5 区间 的元素

    NSArray *fliterArray = [array filteredArrayUsingPredicate:predicate];

    [fliterArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSLog(@"Predicate范围运算功能 fliterArray = %@",obj);

    }];

    

    

}



#pragma  mark 4. Predicate 与自身相比的功能

- (void)testPredicateComparationToSelf{

    /*

     (3)字符串本身:SELF

     例:@“SELF == ‘APPLE’"

     */

    

    NSArray *placeArray = [NSArray arrayWithObjects:@"Shanghai",@"Hangzhou",@"Beijing",@"Macao",@"Taishan", nil];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == 'Beijing'"];

    NSArray *tempArray = [placeArray filteredArrayUsingPredicate:predicate];

    [tempArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSLog(@"Predicate 与自身相比的功能 obj == %@",obj);

    }];

    

}


//  @"name CONTAIN[cd] 'ang'"  <实际应用:对NSArray进行过滤>

#pragma mark 5. Predicate 字符串相关:BEGINSWITHENDSWITHCONTAINS

- (void)testPredicateRelateToNSString{

    /*

     (4)字符串相关:BEGINSWITHENDSWITHCONTAINS

     例:@"name CONTAIN[cd] 'ang'"   //包含某个字符串

     @"name BEGINSWITH[c] 'sh'"     //以某个字符串开头

     @"name ENDSWITH[d] 'ang'"      //以某个字符串结束

     :[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。

     */

    NSArray *placeArray = [NSArray arrayWithObjects:@"Shanghai",@"Hangzhou",@"Beijing",@"Macao",@"Taishan", nil];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] 'an' "];

    // NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF Beginswith [cd] 'sh' "];

    

    NSArray *tempArray = [placeArray filteredArrayUsingPredicate:predicate];

    [tempArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSLog(@"Predicate 字符串相关:BEGINSWITHENDSWITHCONTAINS obj == %@",obj);

    }];

    

}



#pragma mark 6. Predicate 的通配

- (void)testPredicateWildcard{

    /*

     (5)通配符:LIKE

     例:@"name LIKE[cd] '*er*'"

     //  *代表通配符,Like也接受[cd].

     @"name LIKE[cd] '???er*'"

     */

    NSArray *placeArray = [NSArray arrayWithObjects:@"Shanghai",@"Hangzhou",@"Beijing",@"Macao",@"Taishan", nil];

//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF  like '*ai*' "];

    NSString *str = [NSString stringWithFormat:@"SELF  like '*%@*' ",@"ai"];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:str];


//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] '???er*'"];//不知道为什么报错


    NSArray *tempArray = [placeArray filteredArrayUsingPredicate:predicate];

    [tempArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSLog(@"obj == %@",obj);

    }];

    

    [self testPredicateRegex];

    [self testThid];

}


//1)NSArray进行过滤

- (void)testOne

{

    NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil];

    NSString *string = @"ang";

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string];

    NSLog(@"%@",[array filteredArrayUsingPredicate:pred]);


}


//(2)判断字符串首字母是否为字母:

- (void)testPredicateRegex {

    /*

    (6)正则表达式:MATCHES

    例:NSString *regex = @"^A.+e$";   //A开头,e结尾

    @"name MATCHES %@",regex

    */

    

    NSString *regex = @"[A-Za-z]+";

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    

    BOOL isMATCHES = [predicate evaluateWithObject:@"1ddadfa"];

    if (isMATCHES) {

        NSLog(@"isMATCHES = %d",isMATCHES);

    }

}


//(3)字符串替换

- (void)testThid

{

    //组装一个字符串,需要把里面的网址解析出来

    NSString *urlString=@"<meta/><link/><title>1Q84 BOOK1</title></head><body>";

    

    //NSRegularExpression类里面调用表达的方法需要传递一个NSError的参数。下面定义一个

    NSError *error;

    

    //http+:[^\\s]* 这个表达式是检测一个网址的。(?<=title\>).*(?=</title)截取html文章中的<title></title>中内文字的正则表达式

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=title\\>).*(?=</title)" options:0 error:&error];

    

    if (regex != nil) {

        NSTextCheckingResult *firstMatch=[regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];

        

        if (firstMatch) {

            NSRange resultRange = [firstMatch rangeAtIndex:0];

            

            //urlString当中截取数据

            NSString *result=[urlString substringWithRange:resultRange];

            //输出结果

            NSLog(@"->%@<-",result);

        }    

        

    }


}