3

As a Javascript programmer it has been drummed into my head to use === instead of == where ever possible.

As I'm learning Objective C, even in the official documentation, I only ever see == being used.

My question is, should I continue my habits of using strict equality in my Objective C code? Is it as necessary as it is with Javascript? I would assume strict equality gives a slight performance boost, but in Objective C is this boost too slight to make much of a difference?

Jimmery
  • 9,783
  • 25
  • 83
  • 157
  • see this.. http://stackoverflow.com/questions/2293859/checking-for-equality-in-objective-c and http://stackoverflow.com/questions/8020776/how-to-test-equality-in-objective-c – DharaParekh May 15 '13 at 09:36
  • 1
    The reason you use `===` in Javascript is only because its `==` operator is broken beyond belief. The `==` operator in Objective-C is not broken, but also it almost certainly doesn't do what you want, at least for objects. Two of the answers below explain why and what you should do. – JeremyP May 15 '13 at 09:53

4 Answers4

9

It's very simple, in Objective C, you don't have the === operator.

You usually don't use == to compare objects, because it compares the pointer values, not the value of the objects. So, if you want to compare two strings for example, you should use:

[stringObject isEqual:@"the string"];

This compares the string value, not the pointers. There are however valid reasons to use the == operator to compare objects. Many delegate callbacks have the sender object as a parameter. If you are implementing multiple tableviews with one controller, for instance, you want to check which tableview is calling your method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  if (tableView == self.firstTableView) {
     ...
  }

   if (tableView == self.secondTableView) {
     ...
  }
...
}
Alexander
  • 59,041
  • 12
  • 98
  • 151
Marcel
  • 6,393
  • 1
  • 30
  • 44
2

The strict equality operator (===) exists in JS because the regular equality operator (==) does an implicit type conversion if you are comparing two different data types. One of the arguments will be converted to match with the other based on a set of unintuitive rules. For example, 1 == TRUE will give you 'true' even though 1 is a number and TRUE is a boolean. Does anybody know if Objective-C does implicit type conversions like this?

stefvhuynh
  • 166
  • 10
1

AFAIK there is no equivalent to === in Objective-C.

You can compare int with float using ==, which is not a good idea for other reasons. When you compare two ids (eg. UIView* and UIButton*) with == then you compare the pointer values. If that is TRUE/YES then it is an identical, not even just equal, object. It is the same instance.

When comparing objects for equality of their values then you are far better of by using the isEqual: method. You may have to overwrite isEqual in your own subclasses in order to use it properly with your own object.

If you had some concrete problem/excample then we could explain it better, I think.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
1

Operator like === do not exist in Objective C.

And basically there is one general rule if you want to compare two objects: Use isEqual: and don't use == for objects. Unless you are pretty sure, what you are doing.

Here quite good link to get in depth knowledge: ObjectComparison

JeremyP
  • 84,577
  • 15
  • 123
  • 161
Vytautas
  • 573
  • 3
  • 8