【发布时间】:2014-09-18 16:19:36
【问题描述】:
我正在尝试使用正则表达式验证 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}"'
为什么不这样?
【问题讨论】:
标签: ios regex nspredicate