大小写不敏感比较:与是大小写敏感点赞:之间的不一致



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

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 的大小写不敏感比较:表示两个字符串不相等
NSComparisonMethods protocol's isCaseInsensitive Like: say aBunchOfAsterisk等于空字符串
NSComparison方法 protocol's isCaseInsensitiveLike: say emptyString NOT EQUAL TO aBunchOfAsterisk

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

[emptyString isCaseInsensitiveLike:aBunchOfAsterisks];

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

从标题:

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

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

最新更新