Re: Wisdom of overriding isEqual:
Re: Wisdom of overriding isEqual:
- Subject: Re: Wisdom of overriding isEqual:
- From: Patrick Machielse <email@hidden>
- Date: Fri, 12 Mar 2004 00:33:01 +0100
op 11-03-2004 17:47 schreef Bill Cheeseman
>
Equality of strings is described in the -isEqualToString: documentation, and
>
it is almost certain that -isEqual: calls this. Ditto for NSData,
>
NSDictionary, etc.
>
>
Are there any Foundation data classes that don't explain what their
>
-isEqualTo...: method does?
Look at the container classes:
[NSArray isEqualToArray:]
-------------------------
Two arrays have equal contents if they each hold the same number of objects
and objects at a given index in each array satisfy the isEqual: test.
-------------------------
[NSArray containsObject:]
-------------------------
Returns YES if anObject is present in the array. This method determines
whether an object is present in the array by sending an isEqual: message to
each of the array9s objects (and passing anObject as the parameter to each
isEqual: message).
-------------------------
This is clear, and what I expect from the conceptual documentation: NSArrays
use the isEqual: message to check for object equality. Now look at analogous
methods of NSSet:
[NSSet isEqualToSet:]
---------------------
Two sets have equal contents if they each have the same number of members
and if each member of one set is present in the other.
---------------------
[NSSet containsObject:]
-----------------------
Returns YES if anObject is present in the receiver, NO otherwise.
-----------------------
To the unexpecting reader this _must imply_ pointer comparison of the
objects in NSSets...
This is just sloppyness in the documentation causing confusion. I think the
documentation of addObject: in NSMutableSet provides an interesting look
into this:
[NSMutableSet addObject:]
-------------------------
Adds the specified object to the receiver if it is not already a member.
anObject is sent a retain message as it is added to the receiver. If
anObject is already present in the set, this method has no effect on either
the set or anObject.
-------------------------
So there it is; an object is 'present' in the set if it is a member (some
object in the set returns YES for isEqual:). I think this mixing presence
(as established in my mind as pointer equivalence) and membership (as tested
by isEqual:) is extremely unlucky. The terms 'equality' and 'presence' are
used quite loosely throughout the documentation, when they shouldn't.
By the way, the documentation you pointed out did indeed indicate (but not
say) that isEqualTo.. gets called by isEqual: in the standard classes. The
example seems to illustrate a trade-off: providing the faster isEqualTo:
implies slowing down isEqual: (unless you share implementation with a
macro). Some more guidence on _when_ to provide isEqualTo: and how to
correctly subclass Foundation objects other than NSObject would be nice.
Groeten,
Patrick
--
Hieper Software
w: www.hieper.nl
e: email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.