Sending isEqual: to nil always returns NO

后端 未结 3 538
栀梦
栀梦 2021-01-02 07:58

If you send isEqual: to an object that happens to be nil, you always get NO back.

Is this the expected behavior? To be a feature instead of a bug, I would expect i

3条回答
  •  孤城傲影
    2021-01-02 08:24

    It is expected, for two reasons: (1) in Objective-C, sending a message to nil always returns a false-y value (nil, NO, 0, 0.0, etc.; or, more generally speaking, 0, which can be interpreted based on the expected return type of the method); (2) nil represents an unknown value, and two unknown values are not necessarily equal to each other.

    If you want to see if an object is nil, use if (!obj) or if (obj == nil).

提交回复
热议问题