What is the difference between compare: and isEqualToString :?

I worked 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 feel free to.
Thank you very much.

+4
source share
3 answers

The compare method allows you to determine the order of objects so that you can use it to sort. IsEqualToString: just to determine if two strings have the same value (note: it compares the value, not the objects).

+7
source

isEqualToString: in particular, checks if two strings are equal. This method is extended to compare strings and only check if two strings are equal (that is, they are the same).

compare: is a general method for comparing two objects and is not necessarily improved for strings. compare: also returns the relative position of two objects, not only whether they are equal, but rather they are less than or equal to or greater than the object to which they are compared.

+2
source

compare will provide you with an NSComparisonResult, which you can use to order material inside a tableView, such as NSOrderedSame, or NSOrderedAscending, etc.

isEqualTo is an NSObject method that should be extended to include subclasses, such as NSString (isEqualToString :), basically it compares the object with another object as you would expect it with content. [@ "d" isEqualTo: @ "d"] will return TRUE or 1

0
source

All Articles