Anonymous White
Anonymous White

Reputation: 2159

Does Apple Provide default implementation of isEqual:

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.

Upvotes: 0

Views: 836

Answers (3)

CRD
CRD

Reputation: 53000

As NSObject provides isEqual:, and all your objects are descendants of NSObject, 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 algorithms isEqual: could use:

  1. Pointer comparison - is it the exact same object
  2. Shallow comparison - do the fields of the object have the same value compared directly
  3. Deep comparison - do the non-pointer-valued fields compared directly have the same value, and do the pointer-valued fields compare equal using 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:

@implementation A

- (BOOL) isEqual:(id)other
{
   NSLog(@"A.isEqual called");
   return self == other; // true iff same object
}

@end

@interface B

@property (readwrite) int anInteger;
@property (readwrite) A *anA;

@end

@implementation B

@synthesize anInteger, anA;

@end

// Let's test the algorithm

A *myA = [A new];
B *bOne = [B new];
B *bTwo = [B new];

bOne.anInteger = 42;
bOne.anA = myA;

bTwo.anInteger = 42;
bTwo.anA = myA;

// What output is produced (all of it!)
NSLog(@"[bOne isEqual:bTwo] -> %@", [bOne isEqual:bTwo] ? @"Yes" : @"No");

HTH a little.

Upvotes: 1

deleted_user
deleted_user

Reputation: 3805

Its a little confusing because of the way Apple separates their docs between protocols and interfaces.

@protocol NSObject

- (BOOL)isEqual:(id)object;

This is a required method to be implemented so NSObject (the class) definitely implements this although you wouldnt know it from looking at the class definition on apples dev site. This is directly from the headers in xcode.

In general without implementing a custom isEqual you will expect to only get pointer identity and thats ok in many cases. Systems need to be designed around the way you identify unique instances regardless of the peculiarity of a particular feature such as hash and isEqual. If you need to test for object equality beyond the pointer then you just have to do that.

Upvotes: 2

zoul
zoul

Reputation: 104065

I think that NSObject’s implementation does pointer comparison, and various other classes from the SDK do what’s most appropriate, ie. NSString does comparison on string contents, NSArray compares content equality, and so on. If you want to have “better” equality defined for your custom objects, you have to decide about the semantics and implement it yourself.

Upvotes: 2

Related Questions