2016-03-09 17 views
23

Tôi đang cố gắng tìm ra những gì isLike: thực sự làm cho NSString và gặp sự cố. Của Apple own documentation rất mơ hồ:NSString's isLike: thực sự làm gì?

Trả về giá trị Boolean cho biết người nhận có "giống như" một đối tượng nhất định không.

...

Việc thực hiện mặc định cho phương pháp này được cung cấp bởi NSObject phương pháp trả NO. NSString cũng cung cấp việc thực hiện phương pháp này, trả về YES nếu người nhận khớp với mẫu được mô tả bởi đối tượng .

Nó đề cập đến một "mẫu", nhưng với một số thử nghiệm thô sơ, nó có vẻ như không sử dụng cụm từ thông dụng. Định dạng mẫu chính xác trong trường hợp này là gì?

+2

Ứ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)? –

+0

@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

+1

@Earlz, tôi đã thử '[@" FooBar "isLike: @" Foo "] 'nhưng có kết quả sai. – cdlane

Trả lời

10

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ợ ?* 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ớ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 ]- trong lớp học.

8

Tiêu đề NSScriptWhoseTest.h cung cấp thêm một chút thông tin:

@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 
+1

Cùng với liên kết @JoshCaswell tới [NSPredicate] (https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html#//apple_ref/doc/uid/TP40001795-215868), có thể là '*' và '?' là các ký tự mẫu duy nhất. Lưu ý rằng '\' cũng được cho phép để thoát: '[@" F? Oo "isLike: @" \\ F \\? Oo "]' là 'true' –

+0

Tôi phát hiện ra đôi khi sau đó điều này là không đầy đủ. Nó cũng cho phép các lớp ký tự, ví dụ 'isLike:" [abc] foobar "' sẽ trả về true cho "afoobar" cũng như "cfoobar" – Earlz

Các vấn đề liên quan