0
我想使用正則表達式在iOS中驗證用戶名。爲此,我使用NSPredicate,但它引發了運行時錯誤。ios:使用NSPredicate檢查正則表達式 - 無法解析格式字符串
下面是我在我的ViewController:
@property (nonatomic, strong) NSString *userNamePattern;
和:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_userNamePattern = @"[a-zA-Z0-9_-]{3,20}";
然後在動作上的一個按鈕,當點擊:
NSPredicate *userNameTest = [NSPredicate predicateWithFormat:self.userNamePattern];
那個底線是拋出此錯誤:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "[a-zA-Z0-9_-]{3,20}"'
爲什麼不是這樣?
嗯,就是這樣。謝謝! – user1513171 2014-09-18 16:43:16