Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between compare: and isEqualToString:?

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.

like image 331
Dives Avatar asked Jan 21 '23 06:01

Dives


1 Answers

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).

like image 90
Denis Hennessy Avatar answered Mar 04 '23 11:03

Denis Hennessy