1

我想知道是否有人可以解释这种看似奇怪的行为?

NSString *emptyString = @"";
NSString *aBunchOfAsterisks = @"*********";

NSLog(@"NSString's caseInsensitiveCompare: says the two strings are %@", [emptyString caseInsensitiveCompare:aBunchOfAsterisks] == NSOrderedSame ? @"EQUAL" : @"NOT EQUAL");
NSLog(@"NSComparisonMethods protocol's isCaseInsensitiveLike: says aBunchOfAsterisks is %@ to emptyString", [emptyString isCaseInsensitiveLike:aBunchOfAsterisks] ? @"EQUAL" : @"NOT EQUAL");
NSLog(@"NSComparisonMethods protocol's isCaseInsensitiveLike: says emptyString is %@ to aBunchOfAsterisks", [aBunchOfAsterisks isCaseInsensitiveLike:emptyString]? @"EQUAL" : @"NOT EQUAL");

记录以下内容


NSString的caseInsensitiveCompare:说这两个字符串不
相等

为什么是第二种比较方法,即

[emptyString isCaseInsensitiveLike:aBunchOfAsterisks];

返回是?更糟糕的是,这似乎只有在字符是“ * ”时才会发生。aBunchOfAsterisks中的任何其他值以及所有 3 种比较方式,都返回NO

4

1 回答 1

2

从标题:

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

因此你的*********字符串减少到*,因此它匹配空字符串......

于 2013-11-07T16:57:24.200 回答