NSObject isEqual: vs isEqualTo:

Got confused about these 2 methods.

NSObject protocol has an isEqual: method.

NSObject implements NSObject protocol but it does not have isEqual:, instead it has an isEqualTo: method.

What's the magic behind the design? Or is the doc wrong?

Should I implement both methods in a derived class?

Answered by Claude31 in 747328022

The subtle difference is explained here: https://stackoverflow.com/questions/7096691/difference-between-isequalto-and-isequal

It is also explained what you should override or not.

isEqual: is part of the NSObject protocol and is meant for comparing objects.  isEqualTo: is part of the Cocoa AppleScript support infrastructure (specifically, NSComparisonMethods, which allow AppleScript to compare Cocoa objects). It's normally the same as isEqual:, but can be overridden if you want equality to work differently internally and in a script.

What a trap :( I overwrote isEqual(to:) by accident, and has a test failure that took me a while to catch.

Accepted Answer

The subtle difference is explained here: https://stackoverflow.com/questions/7096691/difference-between-isequalto-and-isequal

It is also explained what you should override or not.

isEqual: is part of the NSObject protocol and is meant for comparing objects.  isEqualTo: is part of the Cocoa AppleScript support infrastructure (specifically, NSComparisonMethods, which allow AppleScript to compare Cocoa objects). It's normally the same as isEqual:, but can be overridden if you want equality to work differently internally and in a script.

What a trap :( I overwrote isEqual(to:) by accident, and has a test failure that took me a while to catch.

NSObject isEqual: vs isEqualTo:
 
 
Q