16

Possible Duplicate:
Comparing objects in Obj-c

What the difference is between these two methods of checking for object equality:

UIButton *btn1 = [[UIButton alloc] init];
UIButton *btn2 = [[UIButton alloc] init];

What is the difference between:

if (btn1 == btn2) {
  // Run some code
}

and

if ([btn1 isEqual:btn2]) {
  // Run some code
}
Community
  • 1
  • 1
Matt H.
  • 10,438
  • 9
  • 45
  • 62

3 Answers3

20

The first way compares pointers, while the second way compares objects.

That is, the first way compares if the pointers have the same value. In this case it is likely that they don't, in the second case the objects will be compared. Since they are initialized the same way they could be equal. (Note, it appears that with the UIButton's implementation of isEqual: the result is always false.)

In most cases using == is not what you want. However, what is appropriate depends on your objective.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
  • 5
    **objects are likely to be equal??** how? they will be entirely different objects.. – Krishnabhadra Dec 26 '12 at 06:13
  • 1
    He answered your question pretty well. Maybe you could clarify what part you want to understand better, or give an example of what you're trying to accomplish. – paulmelnikow Dec 26 '12 at 06:13
  • 1
    @noa I updated my answer from my original one. My original answer was brief. – ThomasW Dec 26 '12 at 06:14
  • 1
    downvote for saying **in the second case the objects will be compared. Since they are initialized the same way they are likely to be equal.**.. From the question, no way `[btn1 isEqual:btn2]` to return `true`. So they are **not equal** – Krishnabhadra Dec 26 '12 at 06:20
  • 2
    @Krishnabhadra I've updated my answer. The implementation of `isEqual:` depends on the class. In the case of `UIButton` (and most likely `UIView`) it doesn't return true for different instances. However, in other cases, for example with `NSMutableString`, if the value of the strings are equal then the method will return true. – ThomasW Dec 26 '12 at 06:29
  • 1
    Just to let you know, the default IMPL of isEqual compares object hashes (which means objects created, archived, then dearchived (NSCoding) should be equal. – CodaFi Dec 26 '12 at 06:38
  • Instead of "pointers" and "objects" you may want to use the technical terms *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:40
15

Prateek's answer and Thomas's edited answer is correct. But I just want to add a common pitfall/confusion when dealing with this type of cases..

Consider this case

 NSString *str1  = [[NSString alloc] initWithString:@"hello"];
 NSString *str2  = [[NSString alloc] initWithString:@"hello"];

Ideally str1 and str2 should be 2 different string objects, str1 and str2 should be pointing to different addresses. But running below code prints str1 == str2

if(str1 == str2){
    NSLog(@"str1 == str2");
}

and below code prints str1 isEqual str2

if([str1 isEqual:str2]){
    NSLog(@"str1 isEqual str2");
}

The reason is, the two identical string literal passed through initWithString will have the same address to start, so they are the same object too (See this). This is the optimization of constant data, which is a feature in iOS (and many other implementation I feel).

But this won't work for other kind of objects/classes. When you create 2 UIButton they will entirely different objects and both btn1 and btn2 (see the question) will points to different address.

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • 1
    On 64 bit systems, this happens actually in many cases. NSNumber, NSIndexPath, NSDate, and NSString with short values often have the same pointer when they are equal. There is only one [NSNull null], only one @ [], only one @ YES and one @ NO. – gnasher729 Nov 25 '15 at 14:21
5

Mentioned difference in comments

if (btn1 == btn2) //This compared your pointers
{
  // Run some code
}

and

if ([btn1 isEqual:btn2]) //Compares UIButton object
{
  // Run some code
}
P.J
  • 6,547
  • 9
  • 44
  • 74