Foundation: -isEqual: and -hash behaviour for NSCFBoolean incorrect.
||Date Originated:||05-May-2006 04:29 AM|
|Product:||Mac OS X
The documentation for NSObject states that: "If two objects are equal, they must have the same hash value." However, the Foundation classes NSCFNumber and NSCFBoolean do not honour this. Specifically, -isEqual: for these classes returns YES when comparing the integer NSNumber 1 with the boolean NSNumber true, but their hashes differ.
Steps to Reproduce:
Create an NSNumber object with the integer value 1. Create an NSNumber with the boolean value YES. Compare the two with isEqual:, and compare their hashes. (Code attached)
There are two possible correct behaviours: either isEqual: should return YES and the hashes should be identical, or isEqual: should return NO and equality of hash values would not be an issue.
isEqual: returns YES, but the hashes differ. Specifically, for the integer NSNumber, hash returns 1, while the hash for the boolean NSNumber is its address (inherited from the NSObject implementation).
Core Foundation’s equivalents handle this by returning FALSE from CFEqual(). This does not violate the definitions of CFEqual() and CFHash(), but is less convenient than defining the values to be equal. A better fix would be for the hash of NSCFBoolean to be 0 or 1 as appropriate.
Also note that this different behaviour means that -isEqual: and CFEqual() return different values for the same toll-free bridged core objects, which breaks expectations.
The fix that would have the least effect on existing software would be to change the hash of NSCFBoolean and continue to return different values for -isEqual: and CFEqual() despite the unexpectedness of this difference.
This bug was mentioned at <http://www.mulle-kybernetik.com/weblog/archives/000733.html>; the author does not specify whether he filed a bug and does not cover the CoreFoundation aspects.
Reports posted here will not necessarily be seen by Apple.
All problems should be submitted at bugreport.apple.com before they are posted here.
Please only post information for Radars that you have filed yourself, and please do
not include Apple confidential information in your posts. Thank you!