Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString: isEqual vs. isEqualToString

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: ?

like image 512
Jaka Jančar Avatar asked Aug 18 '09 09:08

Jaka Jančar


2 Answers

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:

Special Considerations

When you know both objects are strings, this method is a faster way to check equality than isEqual:.

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

like image 160
Abizern Avatar answered Sep 21 '22 15:09

Abizern


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>:

like image 29
Jonathan Dann Avatar answered Sep 21 '22 15:09

Jonathan Dann