前言
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(@"%@
",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];
將兩個手勢依次處理, 一個校驗失敗,再執行另外一個手勢的校驗,以解決手勢衝突.