/ / NSPredicate regex não funciona - objetivo-c, ios, regex, cocoa-touch, nspredicate

NSPredicate regex não está funcionando - objective-c, ios, regex, cacau-toque, nspredicate

Estou a usar:

+ (BOOL)isPassword:(NSString*)password {
NSString* pattern = @"^(?=.{6,20}$).*$";
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
return [predicate evaluateWithObject:password];
}

Mas está voltando sim para "". Alguma dica?

Respostas:

2 para resposta № 1

Como @Pfitz apontou, você não tem um SELF. Isso é usado ao filtrar arrays, por exemplo.

Tente usar NSRegularExpression em vez de.

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"^(?=.{6,20}$).*$"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:password
options:0
range:NSMakeRange(0, [password length])];
if (match) {
NSRange range = [match range];
if (range.location != NSNotFound) {
// match
}
}