Take the 2-minute tour ×

How does one compare objects in Objective-C?

Is it as simple as == ?

I want to check an array for an object and if it doesnt exist add it to the array otherwise, remove it from the array.

share|improve this question

3 Answers 3

up vote 11 down vote accepted

Comparing objects in Objective-C works much the same as in Java or other object-oriented languages:

  • == compares the object reference; in Objective-C, whether they occupy the same memory address.
  • isEqual:, a method defined on NSObject, checks whether two objects are "the same." You can override this method to provide your own equality checking for your objects.

So generally to do what you want, you would do:

if(![myArray containsObject:anObject]) {
    [myArray addObject:anObject];

This works because the Objective-C array type, NSArray, has a method called containsObject: which sends the isEqual: message to every object it contains with your object as the argument. It does not use == unless the implementation of isEqual: relies on ==.

If you're working entirely with objects that you implement, remember you can override isEqual: to provide your own equality checking. Usually this is done by comparing fields of your objects.

share|improve this answer
:) Great. Thanks a lot for this. I am using == and it seems to work. What is more efficient? Could I encounter any problems with the ==? I will use containsObject, but I would just like to know. –  Helium3 Feb 20 '11 at 2:23
== is generally more efficient as it's a single arithmetic comparison, rather than a (potentially extensive) custom comparison involving a method call, but using isEqual: is much more reliable and will almost always deliver better results. It's more forward-compatible too - if you need a new isEqual: implementation in the future, not only will you have to write it, you'll need to recode all your == as isEqual: at that point. If you do it now, you can just implement isEqual: to use == and change it later as needed. –  Tim Feb 21 '11 at 2:27
The relevant technical terms here are identity and equality. == asks if they are identical (are they the same object instance) while isEqual asks if they are equal (where sometimes, depending on type semantics, two separate instances are considered equal). –  AlexChaffee Jan 22 '13 at 23:39

Every Objective-C object has a method called isEqual:.


So you would want to override this for your custom object types.

One particular important note in the documentation:


share|improve this answer
The method is isEqual: (with a colon). The colon makes a huge difference. –  dreamlax Feb 20 '11 at 10:50
+1 for adding the comment about overriding the hash. It seems that containsObject will use the hash method and ignore isEqual: in certain situations. –  Mike M Nov 13 '13 at 17:37

== will compare the pointer, you need to override

- (BOOL)isEqual:(id)anObject
share|improve this answer
I think you meant 'override', not 'overload'. –  jlehr Feb 20 '11 at 20:57

Your Answer


