NSObject isEqual: 和 hash 默认函数是什么?
我有一个 NSObject 数据库模型类。 我在 NSMutableArray 中有一组这些对象。 我使用 indexOfObject:
来查找匹配项。 问题是模型对象的内存地址发生变化。 因此,我重写 hash
方法以返回模型的行 ID。 但这并不能解决问题。 我还必须重写 isEqual: 方法来比较 hash 方法的值。
isEqual:
方法默认使用什么来确定相等性?
我假设它使用内存地址。 阅读 isEqual:
文档 我认为它使用了 hash
方法中的值。 显然,情况并非如此,因为我尝试覆盖该值并没有解决我最初的问题。
I have a database model class that is a NSObject
. I have a set of these objects in a NSMutableArray
. I use indexOfObject:
to find a match. Problem is the model object's memory address changes. So I am overriding the hash
method to return the model's row ID. This however does not fix it. I also have to override the isEqual:
method to compare the value of the hash
method.
What does the isEqual:
method use to determine equality by default?
I'm assuming it uses the memory address. After reading the isEqual:
documentation I thought it used the value from the hash
method. Obviously, that is not the case as my attempt to override that value did not solve my initial problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您所猜测的,
NSObject
的默认isEqual:
行为是比较对象的内存地址。 奇怪的是,目前没有在 NSObject 类参考,但它记录在 内省 文档,其中指出:当然,您无疑知道,
NSObject
的子类可以重写isEqual:
以实现不同的行为。 例如,NSString
的isEqual:
方法,当传递另一个NSString
时,将首先检查地址,然后检查两者之间的精确文字匹配。琴弦。As you've correctly guessed,
NSObject
's defaultisEqual:
behaviour is comparing the memory address of the object. Strangely, this is not presently documented in the NSObject Class Reference, but it is documented in the Introspection documentation, which states:Of course, as you are doubtless aware, subclasses of
NSObject
can overrideisEqual:
to behave differently. For example,NSString
'sisEqual:
method, when passed anotherNSString
, will first check the address and then check for an exact literal match between the strings.关于
isEqual:
默认实现的答案是全面的。 所以我只是添加关于hash
默认实现的注释。 就是这样:即,它与
isEqual:
中使用的指针值相同。 您可以通过以下方式进行检查:结果将如下所示:
Best Regards。
顺便说一句,iOS 8 中的
hash
成为了一个属性而不是一个方法,但它确实存在。The answer about default implementation of
isEqual:
is comprehensive one. So I just add my note about default implementation ofhash
. Here it is:I.e it's just the same pointer value which is used in
isEqual:
. Here's how you can check this out:The result will be something like this:
Best Regards.
BTW in iOS 8
hash
became a property not a method, but it's there.我假设
NSObject
isEquals
使用==
运算符,而hash
使用内存地址。isEquals
方法永远不应该使用hash
作为相等性的绝对测试。 如果您搜索足够多的对象(只需创建超过 2^32 个不同的对象,并且其中至少两个具有相同的hash< /代码>)。
换句话说,
hash
需要以下规范:如果两个对象相等,则它们的hash
也需要相等; 但是,如果两个对象的hash
值相等,则它们不一定相等。作为提示,您始终应该一起覆盖
isEquals
和hashCode
。I would assume that
NSObject
isEquals
uses the==
operator, andhash
uses the memory address.isEquals
method should never useshash
as an absolute test for equality. It is guaranteed to have two objects having similarhashCode
, if you search for enough objects (just create more than 2^32 different objects, and at least two of them will have the samehash
).In other words,
hash
requires the following spec: If two objects are equals, then theirhash
needs to be equal; however, if two objects'hash
values are equals, they are not necessarily equal.As a tip, you always should override
isEquals
andhashCode
together.