views:

835

answers:

4

I want to compare if an variable A represents the same object as variable B does.

Could I do that with the == operator?

Or what else is this exactly looking at? I think I need to check for the memory adress of the object where the variable is pointing to, right?

+1  A: 

[objectA isEqual:objectB] is usually a good choice. Note that some classes may have more specialized equality functions. (isEqualToString: et.al.) These generally test not if they are the same object, but if the objects are equal, which is a distinct concept. (Two string objects can be equal, even if they don't have the same memory address.)

Williham Totland
+14  A: 

The == operator tests whether the two expressions are the same pointer to the same object. Cocoa calls this relation “identical” (see, for example, NSArray's indexOfObjectIdenticalTo:).

To test whether two objects are equal, you would send one of them an isEqual: message (or a more specific message, such as isEqualToString:, if it responds to one), passing the other object. This would return YES if you really only have one object (equal to itself, obviously) or if you have two objects that are equal. In the latter case, == will evaluate to NO.

Peter Hosey
So that isEqual method will check all instance variables and/or properties of the object and compare them with another? Or does it only compare the -hash values of NSObject. The -hash value is updated every time a property changes?
HelloMoon
`isEqual:` and `hash` are both intended to be overridden. NSObject's implementation is pretty dumb: IIRC, `-[NSObject isEqual:]` tests pointer equality and `-[NSObject hash]` just returns the object's pointer. (Implementing `isEqual:` on top of `hash` would be a *really bad* idea, because two objects can be inequal and have the same hash. That's called a hash collision.)
Peter Hosey
If you want your objects to have a different idea of equality, you would override **both** `isEqual:` and `hash`. For more information, see the documentation of `isEqual:`: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isEqual:
Peter Hosey
A: 

The two other answers correctly answer the question in your title. The correct answer to the completely different question in your body text, however, is: yes, the == operator is correct for testing whether two variables refer to the same object.

Ahruman
I answered that in the first paragraph of my (revised as of an hour ago) answer.
Peter Hosey
+1  A: 

The == tells you if two pointers are pointing to the same object. isEqual tells you if the contents of two objects are the same (but not necessarily the actual same object). A little confusing.

Try this code to understand it better:

NSString *aString = [NSString stringWithFormat:@"Hello"];
NSString *bString = aString;
NSString *cString = [NSString stringWithFormat:@"Hello"];

if (aString == bString)
 NSLog(@"CHECK 1");

if (bString == cString)
 NSLog(@"CHECK 2");

if ([aString isEqual:bString])
 NSLog(@"CHECK 3");

if ([bString isEqual:cString])
 NSLog(@"CHECK 4");

// Look at the pointers:
NSLog(@"%p", aString);
NSLog(@"%p", bString);
NSLog(@"%p", cString);
nevan
You should use `%p`, not `%i`, for pointers. Printing them in hex helps reveal alignment problems because all common alignment boundaries are powers of 2 and therefore either factors or multiples of 16 (0x10); moreover, a pointer may be bigger than an `int`, so an `int` formatter may lop off part of the address and/or shift it into subsequent formatters.
Peter Hosey
Thanks for the advice, I didn't know about %p.
nevan