Why does this comparison result in NO?
BOOL areTheSame = NSOrderedSame == [@"th" compare:@"They" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 2)];
When I test it on @"th" and @"Th" it’s YES.
What am I missing here?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is counter-intuitive, but the
rangeargument only applies to the receiver. The length of the other string (the argument tocompare:) isn’t range-limited. Your call reduces@"th"to the range {0,2}, which produces@"th"(i.e., this has no effect), and then compares it to@"They".You will see that this:
produces the result you expect, because it cuts the receiver (
@"They") down (to@"Th") and then does the comparison.