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
}
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 ofisEqual:the result is always false.)In most cases using
==is not what you want. However, what is appropriate depends on your objective.