NSDictionary的objectForKey:是否依赖于身份或平等? [英] Does NSDictionary's objectForKey: rely on identity or equality?

查看:111
本文介绍了NSDictionary的objectForKey:是否依赖于身份或平等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个名为 Person 的对象,该对象的属性为 socialSecurityNumber ,并且此类覆盖了 isEqual:方法在社会保险号属性相等时返回true。并说我已经将一堆 Person 的实例放入 NSDictionary 中。



如果我现在实例化一个 newPerson 对象,该对象恰好具有与词典中已有的相同的社会保险号,然后执行 [myDictionary objectForKey:newPerson] ,它将使用 isEqual:并返回YES,还是比较指针并返回否?



我知道我可以编写一个简单的测试来找出答案,但是我想了解 objectForKey到底是什么:在字典中找到匹配项,并且通常在可可中找到一致性(即 NSArray indexofObject:的工作原理相同吗?)

解决方案

NSDictionary 的工作原理哈希表。因此,它同时使用 -hash -isEqual:在字典中查找与给定键相对应的对象。 / p>

因此要回答 NSDictionary 的问题,将使用 isEqual:而不是指针比较。但是除了 isEqual:之外,您还应该在您的 Person hash






字典中的键值对称为条目。每个条目都包含一个代表键的对象和另一个代表键值的对象。在字典中,键是唯一的。也就是说,单个字典中没有两个键是相等的(由isEqual:确定)。







如果两个对象相等,则它们必须具有相同的哈希值。如果您在子类中定义isEqual:并打算将该子类的实例放入集合中,则最后一点特别重要。确保在子类中也定义了哈希。







从索引0开始,向数组的每个元素发送isEqual:消息,直到找到匹配项或到达数组末尾为止。此方法将anObject参数传递给每个isEqual:消息。如果isEqual :(在NSObject协议中声明)返回YES,则对象被视为相等。







您应始终阅读文档:如上述摘录所指出的那样,此类详细信息通常在方法文档的讨论或特殊考虑部分进行解释,或者在类文档本身的概述部分中。


Say I have an object called Person which has the property socialSecurityNumber, and this class overrides the isEqual: method to return true when the social security number properties are equal. And say I've put a bunch of instances of Person into an NSDictionary.

If I now instantiate a newPerson object which happens to have the same social security number as one already in the dictionary, and I do [myDictionary objectForKey:newPerson], will it use the isEqual: and return YES, or will it compare pointers and return NO?

I know I can write a simple test to find out, but I want to understand how exactly objectForKey: finds a match in a dictionary, and generally how consistent this is across Cocoa (i.e. does NSArray's indexofObject: work the same?)

解决方案

NSDictionary works like a hashtable. So it uses both -hash and -isEqual: to find the object in the dictionary corresponding to the given key.

So to answer your question for NSDictionary, this uses isEqual: and not pointer comparison. But you also should implement hash in addition to isEqual: on your Person class for this to work.

A key-value pair within a dictionary is called an entry. Each entry consists of one object that represents the key and a second object that is that key’s value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equal (as determined by isEqual:).

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.

Starting at index 0, each element of the array is sent an isEqual: message until a match is found or the end of the array is reached. This method passes the anObject parameter to each isEqual: message. Objects are considered equal if isEqual: (declared in the NSObject protocol) returns YES.


You should always read the documentation : as pointed out by the extracts quoted above, these kind of details are often explained in the "Discussion" or "Special Consideration" sections of the method documentation or in the "Overview" section of the class documentation itself.

这篇关于NSDictionary的objectForKey:是否依赖于身份或平等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆