I was working on this:
NSString *str1 = @"This is string A";
NSString *str2 = @"This is string B";
NSComparisonResult compareResult;
if([str1 isEqualToString:str2] == YES)
NSLog (@"str1 == str2");
else
NSLog (@"str1 != str2");
compareResult = [str1 compare: str2];
if (compareResult == NSOrderedAscending)
NSLog (@"str1 < str2");
else if(compareResult == NSOrderedSame)
NSLog (@"str1 == str2");
else
NSLog (@"str1 > str2");
So my question is:
what is the difference between compare: and isEqualToString:
I am new to programming, so please bear with.
Thanks a lot.
The compare: method allows you to determine the ordering of the objects so you can use it for sorting. The isEqualToString: is simply for determining whether two strings have the same value (note: it's comparing the value, not the objects).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With