如何比较NSObject的运算符'==' [英] How is comparing operator '==' for NSObject
问题描述
如何比较NSObject
的运算符'==
'?
方法-isEqual:
对我来说很好用,但是当我使用-isEqual
时,我需要检查对象是否存在.使用'==
',我不需要检查它,但是在哪里可以找到它的文档?
How is comparing operator '==
' for NSObject
?
Method -isEqual:
works fine for me, but when I'm using -isEqual
I need to check if objects exists. With '==
' I don't need to check this but where I can find documentation for it?
推荐答案
摘自Apple文档:
返回一个布尔值,该值指示接收方和接收方是否 给定的对象相等. (必需)此方法定义了什么意思 实例是相等的.例如,一个容器对象可能 如果两个容器的对应对象全部相等,则将其定义为相等 对isEqual:请求回答是".参见NSData,NSDictionary, NSArray和NSString类规范的使用示例 这种方法.如果两个对象相等,则它们必须具有相同的哈希值 价值.最后一点特别重要,如果您定义 isEqual:在子类中,并打算放置该子类的实例 进入一个集合.确保您还在子类中定义哈希.
Returns a Boolean value that indicates whether the receiver and a given object are equal. (required) This method defines what it means for instances to be equal. For example, a container object might define two containers as equal if their corresponding objects all respond YES to an isEqual: request. See the NSData, NSDictionary, NSArray, and NSString class specifications for examples of the use of this method. If two objects are equal, they must have the same hash value. This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass.
如果您喜欢这样
if([obj1 isEqual:obj2])
和obj1
,或者obj2
是nil
,那么您将得到NO
. (如果这是您的问题的意思)
-现在
and obj1
, or obj2
is nil
then you will get NO
. (if this is what you meant by your question)
- Now
if(obj1 == obj2)
这是一个指针比较. 指针
This is a pointer comparison. Pointers
这篇关于如何比较NSObject的运算符'=='的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!