ios使用常用正则表达式---NSPredicate

编程故事 2019-06-26

前言

evaluate a predicate against a single object
利用NSPredicate 的evaluateWithObject 方法进行校验

- (BOOL)evaluateWithObject:(id)object;
Description    
Returns a Boolean value that indicates whether a given object matches the conditions specified by the receiver.

例子

#pragma 正则匹配手机号
+ (BOOL)checkTelNumber:(NSString *) telNumber
{
    NSString *pattern = @^1+[3578]+\d{9};
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    return isMatch;
}
  
  
#pragma 正则匹配用户密码6-18位数字和字母组合
+ (BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18};
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
      
}
  
#pragma 正则匹配用户姓名,20位的中文或英文
+ (BOOL)checkUserName : (NSString *) userName
{
    NSString *pattern = @^[a-zA-Z一-龥]{1,20};
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:userName];
    return isMatch;
      
}
  
  
#pragma 正则匹配用户身份证号15或18位
+ (BOOL)checkUserIdCard: (NSString *) idCard
{
    NSString *pattern = @(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$);
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:idCard];
    return isMatch;
}
  
#pragma 正则匹员工号,12位的数字
+ (BOOL)checkEmployeeNumber : (NSString *) number
{
    NSString *pattern = @^[0-9]{12};
      
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:number];
    return isMatch;
      
}
  
#pragma 正则匹配URL
+ (BOOL)checkURL : (NSString *) url
{
    NSString *pattern = @^[0-9A-Za-z]{1,50};
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:url];
    return isMatch;
      
}

【iOS开发】如何使用enumerate开头的方法

  • 1、关于NSString的方法

官方提供的方法:

[objc] view plain copy
/* In the enumerate methods, the blocks will be invoked inside an autorelease pool, so any values assigned inside the block should be retained. 
*/  
- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString * __nullable substring, NSRange substringRange, NSRange enclosingRange, BOOLBOOL *stop))block NS_AVAILABLE(10_6, 4_0);  
- (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOLBOOL *stop))block NS_AVAILABLE(10_6, 4_0);

我们可以用来一行一行的读取一个txt文档的内容,并使用的大笑

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"girl" ofType:@"txt"];  
    NSData *fileData = [NSData dataWithContentsOfFile:filePath];  
    NSString *fileStr = [[NSString alloc]initWithData:fileData encoding:NSUTF8StringEncoding];  
      
    //一行一行的读取  
    [fileStr enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOLBOOL * _Nonnull stop) {  
        NSLog(@"%@\n",line);  
    }];  
      
//    一个字符一字符的读取  
    [fileStr enumerateSubstringsInRange:NSMakeRange(0, fileStr.length) options:NSStringEnumerationByWords usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOLBOOL * _Nonnull stop) {  
        NSLog(@"tmp111===%@",substring);  
    }];

遍历支付密码的例子

  • // 一个字符一字符的读取
//    一个字符一字符的读取
    [fileStr enumerateSubstringsInRange:NSMakeRange(0, fileStr.length) options:NSStringEnumerationByComposedCharacterSequences
                             usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
                                 
                             

        
        NSLog(@"tmp111===%@",substring);
        
        [m_textField appendPsw:substring];
        [_pswView SetInputNum:_inputEncData.count];
        [m_textField onChange];
        
    }];
  • NSStringEnumerationByWords
[fileStr enumerateSubstringsInRange:NSMakeRange(0, fileStr.length) options:NSStringEnumerationByWords
                             usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
                                 
                             

        
        NSLog(@"tmp111===%@",substring);
        
        [m_textField appendPsw:substring];
        [_pswView SetInputNum:_inputEncData.count];
        [m_textField onChange];
        
    }];

OC中的 BOOL不如bool好用

BOOL表示 1是YES , 所以非1是NO. 而bool表示0是false,所以非0是true;

右划返回与scrollView冲突

通过 requireGestureRecognizerToFail方法来处理。

[_bigScrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];

将两个手势依次处理, 一个校验失败,再执行另外一个手势的校验,以解决手势冲突.

相关推荐