In C there is a default implementation of equality operator. Go through all the member and verify that they satisfy the equality operator. The default is somewhat stupid because if an object contains pointer then the equality operator of the member would be performed on the pointer.
Still, it’s good enough for my purpose.
So does it?
Or are we expected to implement isEqual and the corresponding hash for everytime we create a custom object that may we want to use isequal for.
It seems to me the “default” implementation is to simply compare the pointer of the object and not it’s member. Am I correct here? It’s even worse than C++ standard comparison. That’s what I want to verify.
It seems to me if our class is the immediate children of NSObject then isEqual will simply call it’s parent’s isEqual and that simply compare pointers.
Am I correct here? I am just wanting to make sure of that.
As
NSObjectprovidesisEqual:, and all your objects are descendants ofNSObject, then the the simple answer is that a default implementation is provided.Now you are concerned over the algorithm this default uses, and in a comment write “I wouldn’t be sure simply by testing”. Let’s look at testing, just for fun 😉
Now
isEqual:is a rather fundamental method, if Apple decided to change its semantics the consequences could be significant and not good. So while Apple is free to change how it is implemented provided the semantics remain the same, which means the same objects compare equal after the change as before. Now you’ve mentioned three possible algorithmsisEqual:could use:isEqual:These all have different semantics, whichever one Apple has chosen it can’t change without breaking a lot of code. And different semantics means you can test…
Coding as I type, errors expected! Only important bits included:
HTH a little.