tags:

views:

2828

answers:

3

What is the difference between isEqual: and isEqualToString:?

Why are classes adding isEqualTo* methods (isEqualToArray for NSArray, isEqualToData for NSData, ...) instead of just overriding isEqual: ?

+2  A: 

My guess is that it provides a slight performance enhancement, as isEqualToString: won't have to type-check what's passed in.

iKenndac
+9  A: 

isEqual: compares a string to an object, and will return NO if the object is not a string. isEqualToString: is faster if you know both objects are strings, as the documentation states.

isEqualTo<Class> is used to provide specific checks for equality. For instance; isEqualToArray: checks that the number of objects at each index are equal, and that the objects at a given index return YES for the isEqual: test.

Abizern
`isEqual:` returns `BOOL`. It can't return `nil`.
Peter Hosey
Thanks Peter. Fixed.
Abizern
+2  A: 

Also, for writing your own -isEqual: and -isEqualTo<Class>: methods, the convention is to allow nil arguments for -isEqual: and raise an exception for nil arguments to -isEqualTo<Class>:

Jonathan Dann
I hadn't come across this before, any documentation that you know of?
Mike Abdullah
This doesn't seem to be true for isEqualToString, which just returns NO if you pass in nil.
Jaka Jančar
Interesting, it's documented in the Object Comparison section of the <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW31">Cocoa Fundamentals Guide</a>
Jonathan Dann