1

I'm using:

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

But it is returning yes for "". Any tips?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
lolol
  • 4,287
  • 3
  • 35
  • 54

1 Answers1

2

As @Pfitz pointed out, you don't have a SELF. That's used when filtering arrays for instance.

Try using NSRegularExpression instead.

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
    }
}
DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • I can't, it is not iOS 4 compatible. – lolol Oct 10 '12 at 14:16
  • it is iOS 4 compatible, here is the right link: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html – vikingosegundo Oct 10 '12 at 14:18
  • 2
    btw: you should tag you question with iOS if asking for iOS… – vikingosegundo Oct 10 '12 at 14:19
  • I will. By the way, I meant iOS 3.x compatible. My bad. But I like your code and I will use it when and don't need it to work in 3.x. – lolol Oct 10 '12 at 14:21
  • Is there a particular reason why you want to use 3.x? Less then 3% of devices still run 3.x. [Source](http://david-smith.org/blog/2012/03/10/ios-5-dot-1-upgrade-stats/) – DrummerB Oct 10 '12 at 14:24
  • In my country there are something close to 6%. But I think I can convince the boss to drop the support. So I think I'm gonna use your code. – lolol Oct 10 '12 at 14:25
  • Those who still use 3.x probably don't buy any apps anyway, since most of them require 4.x or even 5.x nowadays. – DrummerB Oct 10 '12 at 14:26
  • And please capitalize Cocoa prefixes. Something like "nspredicate" hurts my eyes :) – DrummerB Oct 10 '12 at 14:27
  • Good point. Just another question: is [string length] standing for [password length]? – lolol Oct 10 '12 at 14:28
  • @lolol — and the stats, DrummerB posted are even old. the new stats (http://david-smith.org/iosversionstats/) already indicates >60% for iOS 6 – vikingosegundo Oct 10 '12 at 14:29
  • Yes, @lolol. And true, my stats are outdated. I wanted one where 3.x was explicitly listed. You see, in current statistics it's not even mentioned anymore, that's how old it is. – DrummerB Oct 10 '12 at 14:31
  • I used the same stats few weeks ago to argue (successfully) for not supporting iOS4 anymore… – vikingosegundo Oct 10 '12 at 14:32