NSString是什么样的:实际上呢?

Ear*_*rlz 24 cocoa objective-c nsstring

我试图找出isLike:实际上对NSStrings有什么作用并遇到麻烦.Apple 自己的文档很模糊:

返回一个布尔值,指示接收者是否"喜欢"另一个给定对象.

...

NSObject方法返回的此方法的默认实现NO.NSString还提供了此方法的实现,YES如果接收器匹配object描述的模式,则返回该方法.

它提到了一个"模式",但通过一些初步的测试,它似乎没有使用正则表达式.在这种情况下,模式格式究竟是什么?

neu*_*mer 11

正如其他已经公布,苹果公司的文档不介绍的行为[NSString isLike:]作详细看到这里:

NSObject方法提供的此方法的默认实现返回NO.NSString还提供了此方法的实现,如果接收器匹配object描述的模式,则返回YES.

正如其他人所说,它可能基于NSPredicate.如果是的话它有可能使用NSComparisonPredicate与操作型NSLikePredicateOperatorType的描述在这里:

NSMatchesPredicateOperatorType

完整的正则表达式匹配谓词.

适用于OS X v10.4及更高版本.

NSLikePredicateOperatorType

MATCHES谓词的一个简单子集,与SQL LIKE的行为类似.

适用于OS X v10.4及更高版本.

虽然功能可能是正则表达式的简单子集,但语法肯定是不同的.我在OS X 10.10.5上本地测试了以下内容:

- (NSString *)escapeString:(NSString *)value {
    return [value stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
}

- (void)is:(NSString *)value like:(NSString *)pattern note:(NSString *)note {
    NSLog(@"[@\"%@\" isLike:@\"%@\"] == %@ // %@",
            [self escapeString:value],
            [self escapeString:pattern],
            ([value isLike:pattern] ? @"true" : @"false"),
            note);
}

- (void)testAll {
    // each note contains result on OS X 10.10.5 on 20160503
    [self is:@"foo" like:@"f*" note:@"true, '*' wildcard works like file globbing, not RE"];
    [self is:@"foo" like:@"foo*" note:@"true, '*' is zero or more"];
    [self is:@"foo" like:@"f?o" note:@"true, '?' wildcard works like file globbing, not RE"];
    [self is:@"foo" like:@"f?" note:@"false, not more then one"];
    [self is:@"foo" like:@"f?oo" note:@"false, not less than one"];
    [self is:@"foo" like:@"Foo" note:@"false, is case-sensitive (also see isCaseInsensitiveLike:)"];
    [self is:@"foo" like:@"[Ff]oo" note:@"true, supports character classes"];
    [self is:@"foo" like:@"[^F]oo" note:@"false, does not support RE negation in character classes"];
    [self is:@"foo" like:@"[a-z]oo" note:@"true, supports ranges"];
    [self is:@"foo" like:@"[[:lower:]]oo" note:@"false, does not support POSIX named classes"];
    [self is:@"]oo" like:@"[]]oo" note:@"false, does not support ']' as first character in a class"];
    [self is:@"]oo" like:@"[\\]]oo" note:@"true, backslash to escape interpretation of ']' as end of class"];
    [self is:@"[oo" like:@"\\[oo" note:@"true, backslash to escape interpretation as start of class"];
    [self is:@"-oo" like:@"[x\\-z]oo" note:@"true, supports escape of '-' in character classes"];
    [self is:@"?oo" like:@"\\?oo" note:@"true, escape with backslash"];
    [self is:@"foo" like:@"\\?oo" note:@"false, this is not just wildcard matching"];
    [self is:@"*oo" like:@"\\*oo" note:@"true, escape with backslash"];
    [self is:@"foo" like:@"\\*oo" note:@"false, this is not just wildcard matching"];
    [self is:@"\\foo" like:@"\\\\*oo" note:@"true, escape backslash with another backslash"];
}
Run Code Online (Sandbox Code Playgroud)

这段代码会产生以下结果:

[@"foo" isLike:@"f*"] == true // true, '*' wildcard works like file globbing, not RE
[@"foo" isLike:@"foo*"] == true // true, '*' is zero or more
[@"foo" isLike:@"f?o"] == true // true, '?' wildcard works like file globbing, not RE
[@"foo" isLike:@"f?"] == false // false, not more then one
[@"foo" isLike:@"f?oo"] == false // false, not less than one
[@"foo" isLike:@"Foo"] == false // false, is case-sensitive (also see isCaseInsensitiveLike:)
[@"foo" isLike:@"[Ff]oo"] == true // true, supports character classes
[@"foo" isLike:@"[^F]oo"] == false // false, does not support RE negation in character classes
[@"foo" isLike:@"[a-z]oo"] == true // true, supports ranges
[@"foo" isLike:@"[[:lower:]]oo"] == false // false, does not support POSIX named classes
[@"]oo" isLike:@"[]]oo"] == false // false, does not support ']' as first character in a class
[@"]oo" isLike:@"[\\]]oo"] == true // true, backslash to escape interpretation of ']' as end of class
[@"[oo" isLike:@"\\[oo"] == true // true, backslash to escape interpretation as start of class
[@"-oo" isLike:@"[x\\-z]oo"] == true // true, supports escape of '-' in character classes
[@"?oo" isLike:@"\\?oo"] == true // true, escape with backslash
[@"foo" isLike:@"\\?oo"] == false // false, this is not just wildcard matching
[@"*oo" isLike:@"\\*oo"] == true // true, escape with backslash
[@"foo" isLike:@"\\*oo"] == false // false, this is not just wildcard matching
[@"\\foo" isLike:@"\\\\*oo"] == true // true, escape backslash with another backslash
Run Code Online (Sandbox Code Playgroud)

因此isLike:似乎支持?*喜欢使用反斜杠文件通配以\逃避特殊解释.它还支持使用[]定义范围的字符类-.反斜杠以逃避打开[,反斜杠以逃避]-在类内.


Dar*_*ren 8

标题NSScriptWhoseTest.h提供了更多信息:

@interface NSObject (NSComparisonMethods)
...

- (BOOL)isLike:(NSString *)object;
    // argument should be a string using simple shell wildcards (* and ?).
    // (e.g. "Stev*" or "N?XT").
    // Returns NO if receiver is not an NSString.

- (BOOL)isCaseInsensitiveLike:(NSString *)object;

@end
Run Code Online (Sandbox Code Playgroud)