views:

86

answers:

3

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.

+2  A: 

isEqualToString: specifically tests the equality of two strings. This method is enhanced for string comparisons and only tests if two strings are equal (i.e., that they are the same).

compare: is a generic method for comparing two objects and is not necessarily enhanced for strings. compare: also returns the relative position of two objects, not just whether or not they are equal, but rather whether they are lesser than, equal to, or greater than the object to which they are being compared.

Jason Coco
+4  A: 

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

Denis Hennessy
Thanks for the answer. So what if I wanted to see if two strings had the same characters. Ex: str1 = @"ABCD" and str2 = @"DCBA". I know these are not the same strings but what would I use to see if they had the same characters? Sorry if I am jumping ahead, but this question is lingering in my head.
Dives
Ha. That's worthy of another question all on it's own. There's no single built-in method that will tell you that but it's not difficult to implement. Use an NSMutableArray and add all the letters from the first string. Then iterate over all the letters in the second string and verify that they exist in the array (and remove them). When you're done, verify that the array is empty.
Denis Hennessy
I agree, that's definitely a whole new question. But thanks for the answer. I will come back to it once I start working with arrays. Thanks again.
Dives
A: 

compare will give you an NSComparisonResult, which you can use to order stuff inside a tableView, like NSOrderedSame, or NSOrderedAscending and so on.

isEqualTo is an NSObject method which should be extended overriden by subclasses, like NSString (isEqualToString:), basically it compares an object with another object in a way you would expect it to, with the contents. [@"d" isEqualTo:@"d"] would return TRUE or 1

Antwan van Houdt
`isEqualTo:` is actually for AppleScript comparisons. Methods such as `isEqualToString:`, aside from doing the same thing, are unrelated to it. The completely-generic version is `isEqual:`. (Why there is a separate `isEqualTo:` is beyond me.) Also, the Objective-C constant for true is `YES`.
Peter Hosey