What is the NSObject isEqual: and hash default function?

佐手、 提交于 2019-11-27 17:25:37

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1241575/what-is-the-nsobject-isequal-and-hash-default-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!