美文网首页
Cocoa框架中的NSPredicate用于查询,原理和用法都类

Cocoa框架中的NSPredicate用于查询,原理和用法都类

作者: gezhenrong | 来源:发表于2017-04-28 11:33 被阅读27次

    pragma mark 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*' "];
        
        NSArray *tempArray = [placeArray filteredArrayUsingPredicate:predicate];
        [tempArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"obj == %@",obj);
        }];
    

    pragma mark Predicate 字符串相关:BEGINSWITH、ENDSWITH、CONTAINS

    - (void)testPredicateRelateToNSString{
        /* (4)字符串相关:BEGINSWITH、ENDSWITH、CONTAINS
         例:@"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(@"obj == %@",obj);
        }]; 
    }
    

    pragma mark 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(@"obj == %@",obj);
        }];   
    }
    

    pragma mark Predicate范围运算功能

    - (void)testPredicateRange{
        /*
         (2)范围运算符:IN、BETWEEN
         例:@"number BETWEEN {1,5}"
         @"address IN {'shanghai','beijing'}"
         */
    //    NSArray *array = [NSArray arrayWithObjects:@1,@2,@3,@4,@5,@2,@6, nil];
        NSArray *array = [NSArray arrayWithObjects:@"shanghai",@"beijing", nil];
        //NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF in {2,5}"]; 找到 in 的意思是array中{2,5}的元素
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN {2,5}"];
        NSArray *fliterArray = [array filteredArrayUsingPredicate:predicate];
        [fliterArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"fliterArray = %@",obj);
        }];
    }
    

    pragma mark 测试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 *fliterArray = [array filteredArrayUsingPredicate:predicate];
        [fliterArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"fliterArray = %@",obj);
        }];
    }
    

    相关文章

      网友评论

          本文标题:Cocoa框架中的NSPredicate用于查询,原理和用法都类

          本文链接:https://www.haomeiwen.com/subject/xfvozttx.html