iOS之NSPredicate(正则表达式和UIBarController):谓词

Posted 唐唐_010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS之NSPredicate(正则表达式和UIBarController):谓词相关的知识,希望对你有一定的参考价值。

  • //  实例化对象的头文件

  • @property (nonatomic, strong)NSString *name;
    @property (nonatomic, assign)NSString *sex;
    @property (nonatomic, assign)NSInteger tel;
    @property (nonatomic, assign)NSInteger qq;

    - (Member *)initWithDic:(NSDictionary *)dic;
    + (Member *)memberWithDic:(NSDictionary *)dic;
  • // 构造函数的实现

- (Member *)initWithDic:(NSDictionary *)dic
{
    if (self = [super init])
    {
        _name = dic[@"name"];
        _sex = dic[@"sex"];
        _tel = [dic[@"tel"] integerValue];
        _qq = [dic[@"qq"] integerValue];
    }
    return self;
}

+ (Member *)memberWithDic:(NSDictionary *)dic
{
    return [[self alloc] initWithDic:dic];
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"name = %@, sex = %@, qq = %lu, tel = %lu", _name, _sex, _qq, _tel];
}


  •      // 获取数据源:实例化plist文件

        
        // 不能使用NSBundle获取路径
        NSArray *arrData = [NSArray arrayWithContentsOfFile:@"/Users/Mu/Desktop/谓词和正则表达式/谓词和正则表达式/iosclass(2).plist"];
        
        // 集合存储所有实例化对象:将plist实例化
        //(模型化):取对象的属性进行判断,操作集合和集
        NSMutableArray *arrMember = [NSMutableArray array];
        
        for (NSArray *arrGroup in arrData)
        {
        for (NSDictionary *dicMember in arrGroup)
            {
                [arrMember addObject:[[Member alloc] initWithDic:dicMember]];
            }
        }
        // 字符串需要使用单引号,语句大小写不敏感但属性属于变量大小写敏感
               
        // 1.关系运算
        // - 判断是否(包含判断对象是否相同)
        NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"name == ‘燕江‘ and qq = 287143225"];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate1])
        {
            NSLog(@"1.%@", mem);
        }
        
        // 2.关系运算
        // <=等效=< ; !=和<> ; =>和>= ; !和not; 相等=和==,and和&& ; or和||
        NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"NOT name.length <= 3"];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate2])
        {
            NSLog(@"2.%@", mem);
        }
        
        // 3.CONTAINS
        // - [c] 忽略大小写
        // - [d] 忽略重音符号
        // - [cd]既不区分大小写,也不区分发音符号
        NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"name CONTAINS[cd]%@", @"燕"];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate3])
        {
            NSLog(@"3.%@", mem);
        }

        // 4.BEGINSWITH和ENDSWITH:判断以声明开头或结尾
        NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"name BEGINSWITH %@", @"燕"];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate4])
        {
            NSLog(@"4.%@", mem);
        }
        NSPredicate *predicate5 = [NSPredicate predicateWithFormat:@"name ENDSWITH %@", @"燕"];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate5])
        {
            NSLog(@"4.%@", mem);
        }
        
        // 5.通配符like
        // - *代表一个或者多个或者是空
        // - ?代表一个字符
        NSPredicate *predicate6 = [NSPredicate predicateWithFormat:@"name like %@", @"*江"];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate6])
        {
            NSLog(@"5.%@", mem);
        }


        // 6.关系运算
        // - IN {}: 判断指定集合内对象是否存在(和多个使用OR连接等效)
        NSPredicate *predicate7 = [NSPredicate predicateWithFormat:@"tel IN {18798812696, 18798815221}"];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate7])
        {
            NSLog(@"6.%@", mem);
        }
        NSPredicate *predicate8 = [NSPredicate predicateWithFormat:@"name IN %@", @[@"燕江", @"刘木"]];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate8])
        {
            NSLog(@"6.%@", mem);
        }
        // 7.关系运算
        // - BETWEEN {}: 指定范围,判断数据源是

        NSPredicate *predicate9 = [NSPredicate predicateWithFormat:@"tel BETWEEN {18798812696, 18798815221}"];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate9])
        {
            NSLog(@"7.%@", mem);
        }

        // 8.关系运算
        // - NOT最常见的用法就是从一个数组中剔除另外一个数组的数据
        // - 比起循环比较再加到新数组中,简单的不止一两点。
        NSArray *arrayFilter = @[@"abc1", @"abc2"];
        NSArray *arrayContent = @[@"a1", @"abc1", @"abc4", @"abc2"];
        NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter];
        NSLog(@"8.%@",[arrayContent filteredArrayUsingPredicate:thePredicate]);
        
        // 9.block
        // - 参数evaluatedObject表示数组成员
        // - block必须返回YES或者NO,分别表示匹配还是不匹配
        NSPredicate *predicate10 = [NSPredicate predicateWithBlock:^BOOL(id  _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings)
        {
            Member *mem = (Member *)evaluatedObject;
            if (mem.name.length>4)
            {
                return YES;
            }else
            {
                return false;
            }
        }];
        
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate10])
        {
        NSLog(@"9.%@", mem);
        }
        
        // 10.多重筛选
        // - 如果需要匹配数个属性的筛选,用AND或者OR来串联显然有点麻烦
        // - NSCompoundPredicate可以将多个NSPredicate对象的组合
        // - 组合方式可以是AND或者OR以及NOT:调用类方法创建需要的参数是NSPredicate对象组成的集合
        
        // 11.注意点
        // - %K的K必须是大写: 使用%@ = %@会出现不能匹配
        NSPredicate *predicate11 = [NSPredicate predicateWithFormat:@"%@ = %@", @"name", @"燕江"];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate11])
        {
            NSLog(@"10.%@", mem);
        }
        NSPredicate *predicate12 = [NSPredicate predicateWithFormat:@"%K = %@", @"name", @"燕江"];
        for (Member *mem in [arrMember filteredArrayUsingPredicate:predicate12])
        {
            NSLog(@"10.%@", mem);
        }
        
        
        // 12.正则表达式
        NSPredicate *pri  = [NSPredicate predicateWithFormat:@"SELF MATChES %@",  @"^[1-9](\\d{5,11})"];
        NSLog(@"12.判断是否是qq号:%s", ([pri evaluateWithObject:@"287143225"]==YES)?"YES":"NO");

以上是关于iOS之NSPredicate(正则表达式和UIBarController):谓词的主要内容,如果未能解决你的问题,请参考以下文章

ios:使用 NSPredicate 检查正则表达式 - 无法解析格式字符串

iOS-NSPredicate正则验证

正则表达式和NSPredicate

iOS小技能:NSPredicate在正则表达式的应用下篇

iOS 正则表达式的使用方式

iOS 正则表达式的使用方式