NSObject isEqual: 和 hash 默认函数是什么?

发布于 2024-07-30 13:50:33 字数 587 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人间不值得 2024-08-06 13:50:33

正如您所猜测的,NSObject 的默认 isEqual: 行为是比较对象的内存地址。 奇怪的是,目前没有在 NSObject 类参考,但它记录在 内省 文档,其中指出:

isEqual: 的默认 NSObject 实现只是检查指针是否相等。

当然,您无疑知道,NSObject 的子类可以重写 isEqual: 以实现不同的行为。 例如,NSStringisEqual: 方法,当传递另一个 NSString 时,将首先检查地址,然后检查两者之间的精确文字匹配。琴弦。

As you've correctly guessed, NSObject's default isEqual: 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:

The default NSObject implementation of isEqual: simply checks for pointer equality.

Of course, as you are doubtless aware, subclasses of NSObject can override isEqual: to behave differently. For example, NSString's isEqual: method, when passed another NSString, will first check the address and then check for an exact literal match between the strings.

年华零落成诗 2024-08-06 13:50:33

关于 isEqual: 默认实现的答案是全面的。 所以我只是添加关于 hash 默认实现的注释。 就是这样:

-(unsigned)hash {return (unsigned)self;}

即,它与 isEqual: 中使用的指针值相同。 您可以通过以下方式进行检查:

NSObject *obj = [[NSObject alloc] init];
NSLog(@"obj: %@",obj);
NSLog(@"hash: %x",obj.hash);

结果将如下所示:

obj: <NSObject: 0x16d44010>
hash: 16d44010

Best Regards。

顺便说一句,iOS 8 中的 hash 成为了一个属性而不是一个方法,但它确实存在。

The answer about default implementation of isEqual: is comprehensive one. So I just add my note about default implementation of hash. Here it is:

-(unsigned)hash {return (unsigned)self;}

I.e it's just the same pointer value which is used in isEqual:. Here's how you can check this out:

NSObject *obj = [[NSObject alloc] init];
NSLog(@"obj: %@",obj);
NSLog(@"hash: %x",obj.hash);

The result will be something like this:

obj: <NSObject: 0x16d44010>
hash: 16d44010

Best Regards.

BTW in iOS 8 hash became a property not a method, but it's there.

遇到 2024-08-06 13:50:33

我假设 NSObject isEquals 使用 == 运算符,而 hash 使用内存地址。

isEquals 方法永远不应该使用 hash 作为相等性的绝对测试。 如果您搜索足够多的对象(只需创建超过 2^32 个不同的对象,并且其中至少两个具有相同的 hash< /代码>)。

换句话说,hash 需要以下规范:如果两个对象相等,则它们的 hash 也需要相等; 但是,如果两个对象的 hash 值相等,则它们不一定相等。

作为提示,您始终应该一起覆盖 isEqualshashCode

I would assume that NSObject isEquals uses the == operator, and hash uses the memory address.

isEquals method should never uses hash as an absolute test for equality. It is guaranteed to have two objects having similar hashCode, if you search for enough objects (just create more than 2^32 different objects, and at least two of them will have the same hash).

In other words, hash requires the following spec: If two objects are equals, then their hash 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 and hashCode together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文