Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-[NSObject isEqual:] and -[NSNumber isEqualToNumber:]: what's the difference?

Tags:

objective-c

Both methods return equal results. Of course, -[NSObject isEqual:] doesn't compare the pointers of objects, it somehow check the inner fields or whatever. So what's the point of using exactly -[NSNumber isEqualToNumber:] to compare two NSNumbers?

like image 207
efpies Avatar asked Dec 09 '12 00:12

efpies


2 Answers

From the docs:

Two NSNumber objects are considered equal if they have the same id values or if they have equivalent values (as determined by the compare: method). This method is more efficient than compare: if you know the two objects are numbers.

So it handles id equals and number comparison.

like image 106
Terry Wilcox Avatar answered Sep 19 '22 12:09

Terry Wilcox


They are essentially identical, although isEqual has to do a type check on the class of the object passed in. isEqualToNumber does type checking at compile-time, which is better when possible.

like image 44
EricS Avatar answered Sep 18 '22 12:09

EricS