iOS学习之NSPredicate

Cocoa框架中的NSPredicate类一般用来,筛选查询字符串的匹配,原理和用法类似于SQL语句,作用相当于数据库的查询过滤读取。

NSPredicate初始化方法

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(lastName like[cd] %@) AND (birthday > %@)", lastNameSearchString, birthdaySearchDate];

你可以使用NSPredicate的类方法predicateWithFormat…来直接从一个字符串来创建一个实例对象,上面的谓词同时设置了两个约束查询条件。还有两种初始化方法,一个是直接创建,一个是通过predicate临时变量来创建。另外两种方法并不常用,这里不做过多介绍如需了解,可自行查看官方文档https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html#//apple_ref/doc/uid/TP40001793-CJBDBHCB

Predicate常用的语法格式

  • 比较运算符>,<,==,>=,<=,!=可用于数值及字符串。例:
    @"number > 100"
  • 范围运算符:IN、BETWEEN。例:
    @"number BETWEEN {1,5}"
    @"address IN {'shanghai','beijing'}"
  • 字符串本身:SELF。例:
    @“SELF == ‘APPLE’"
  • 字符串相关:BEGINSWITH、ENDSWITH、CONTAINS。例:
    @"name CONTAIN[cd] 'ang'" //包含某个字符串
    @"name BEGINSWITH[c] 'sh'" //以某个字符串开头
    @"name ENDSWITH[d] 'ang'" //以某个字符串结束
    注:[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。
  • 通配符:LIKE。例:
    @"name LIKE[cd] '*er*'" //*代表通配符,Like也接受[cd].
    @"name LIKE[cd] '???er*'"
  • 正则表达式:MATCHES。例:
    NSString *regex = @"^A.+e$"; //以A开头,e结尾
    @"name MATCHES %@",regex

NSPredicate使用实例

  • 对数组进行筛选

      // 筛选数组中的元素。
      // NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil];    
      // NSString *string = @"ang";    
      // NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string];    
      // NSLog(@"%@",[array filteredArrayUsingPredicate:pred]);
      // array的实例方法
      - (NSArray *)filterArrayWithString:(NSString *)string {
          NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string];    
          return [array filteredArrayUsingPredicate:pred];
      }
    
  • 判断首个字符是否为字母

      // 判断首个字符是否为字母
      - (BOOL)isStartWithWord {
          NSString *regex = @"[A-Za-z]+";    
          NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];    
          return [predicate evaluateWithObject:aString];
      }
    
  • 用户名是否为字母和数字组成

      - (BOOL)isUserName  
      {  
          NSString *regex = @"(^[A-Za-z0-9]{3,20}$)";  
          NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];  
          
          return [pred evaluateWithObject:self];  
      } 
    
  • 密码是否合法

      - (BOOL)isPassword  
      {  
          NSString *regex = @"(^[A-Za-z0-9]{6,20}$)";  
          NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];  
          
          return [pred evaluateWithObject:self];    
      }  
    
  • 邮箱是否合法

      - (BOOL)isEmail  
      {  
          NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";  
          NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];  
          
          return [pred evaluateWithObject:self];  
      }
    
  • url是否合法

      - (BOOL)isUrl  
      {  
          NSString *regex = @"http(s)?:\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- .\\/?%&=]*)?";  
          NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];  
          
          return [pred evaluateWithObject:self];  
      } 
    
  • 电话号码是否合法

      - (BOOL)isTelephone  
      {  
          NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";  
          NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";  
          NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";  
          NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";  
          NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";  
          NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];  
          NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];  
          NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];  
          NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];  
          NSPredicate *regextestphs = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];  
      
          return  [regextestmobile evaluateWithObject:self]   ||  
                  [regextestphs evaluateWithObject:self]      ||  
                  [regextestct evaluateWithObject:self]       ||  
                  [regextestcu evaluateWithObject:self]       ||  
                  [regextestcm evaluateWithObject:self];  
      }

你可能感兴趣的:(iOS学习之NSPredicate)