Như những người khác đã được đăng, tài liệu hướng dẫn của Apple không mô tả hành vi của [NSString isLike:]
một cách chi tiết như đã thấy here:
Việc thực hiện mặc định cho phương pháp này được cung cấp bởi phương pháp NSObject trả về NO. NSString cũng cung cấp việc thực hiện phương thức này, trả về YES nếu người nhận khớp với một mẫu được mô tả bởi đối tượng.
Khi những người khác đề xuất nó có thể dựa trên NSPredicate
. Nếu vậy nó có khả năng được sử dụng NSComparisonPredicate
với nhà điều hành loại NSLikePredicateOperatorType
như mô tả here:
NSMatchesPredicateOperatorType
Một biểu thức chính quy đầy đủ phù hợp với vị ngữ.
Có trong OS X v10.4 trở lên.
NSLikePredicateOperatorType
Một tập con đơn giản của vị MATCHES, tương tự như trong hành vi để SQL tương tự.
Có trong OS X v10.4 trở lên.
Mặc dù chức năng có thể là tập con đơn giản của biểu thức thông thường, cú pháp hoàn toàn khác. Tôi đã thử nghiệm các địa phương sau trên OS X 10.10.5 Hôm nay:
- (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"];
}
Và mã này tạo ra những kết quả này:
[@"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
Vì vậy isLike:
xuất hiện để hỗ trợ ?
và *
như tập tin globbing với dấu chéo ngược \
để thoát khỏi giải thích đặc biệt. Nó cũng hỗ trợ các lớp nhân vật với [
và ]
với phạm vi được xác định bằng -
. Dấu gạch chéo ngược để thoát mở [
và dấu gạch chéo ngược để thoát ]
và -
trong lớp học.
Ứng dụng có khớp với hành vi của [toán tử 'LIKE' từ' NSPredicate'] hay không (https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html # // apple_ref/doc/uid/TP40001795-215868)? –
@JoshCaswell '*' hoạt động như mô tả ở đó, nhưng nó cũng so sánh chuỗi con ở một mức độ nào đó ... vì vậy "FooBar" isLike "Foo" – Earlz
@Earlz, tôi đã thử '[@" FooBar "isLike: @" Foo "] 'nhưng có kết quả sai. – cdlane