lead_the_zeppelin
lead_the_zeppelin

Reputation: 2052

Inconsistencies between caseInsensitiveCompare: vs isCaseSensitiveLike:

I was wondering if anyone could explain this seemingly weird behavior?

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");

which logs the following

NSString's caseInsensitiveCompare: says the two strings are NOT EQUAL
NSComparisonMethods protocol's isCaseInsensitiveLike: says aBunchOfAsterisks is EQUAL to emptyString
NSComparisonMethods protocol's isCaseInsensitiveLike: says emptyString is NOT EQUAL to aBunchOfAsterisks

Why is the second comparison method, namely

[emptyString isCaseInsensitiveLike:aBunchOfAsterisks];

return YES? even worse, this seems to happen only if the character is '*'. Any other value in aBunchOfAsterisks and all 3 ways of comparing, return NO.

Upvotes: 1

Views: 166

Answers (1)

hypercrypt
hypercrypt

Reputation: 15376

From the header:

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

Thus your ********* string reduces down to * and thus it matches the empty string...

Upvotes: 2

Related Questions