我想知道是否有人能解释这种看似奇怪的行为?
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:表示两个字符串不等于
NSComparisonMethods协议的isCaseInsensitiveLike:说 aBunchOfAsterisks与emptyString相等 NSComparisonMethods protocol的isCaseInsensitiveLike:表示emptyString不等于 aBunchOfAsterisks
为什么是第二种比较方法,即
[emptyString isCaseInsensitiveLike:aBunchOfAsterisks];
返回YES?更糟糕的是,这似乎只有在角色为“ * ”时才会发生。 aBunchOfAsterisks 中的任何其他值以及所有3种比较方式,返回否。
答案 0 :(得分: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.
因此,您的*********
字符串缩减为*
,因此它与空字符串匹配...