Re: implementing isEqual: ?
Re: implementing isEqual: ?
- Subject: Re: implementing isEqual: ?
- From: Vince DeMarco <email@hidden>
- Date: Fri, 14 May 2004 11:22:01 -0700
ABRecord's implementation of isEqual: is based only on pointer
equality. Since the framework guarantees that there will be only one
instance of a record at any given time.
vince
On May 14, 2004, at 9:44 AM, Aurilien Hugeli wrote:
hi !
it is the first time i have to implement isEqual: , it seems easy but i
have a problem.
i want to create a class called "MyClass" that is comparable with
ABPerson...
basically, my class only is a wrapper over a NSString*, the UID (unique
ID) of the ABPerson (in fact it is more complex but i simplify here)
i thought it would be cool to be able to "compare" my class instances
with ABPersons to be able to *mix* objects in NSArray etc...
so i wrote this :
// MyClass implementation
[...]
-(BOOL)isEqual:(id)anObject {
if([anObject isKindOfClass:[self class]]) {
return [uniqueId isEqualToString:[anObject getUniqueId]])
}
else if([anObject isKindOfClass:[ABPerson class]])
return [uniqueId isEqualToString:[anObject uniqueId]];
else
return NO; // not compatible with ABPerson
}
i did not forget to implement hash ...
- (unsigned)hash {
return [uniqueId hash];
}
@end
This seems to work in most tests , but i've been suprised to find
strange behaviour like this :
if i have an NSMutableArray of instances of MyClass, when i do
removeObject: and pass it an ABPerson, the MyClass instance *THAT I
KNOW IS IN THE ARRAY* (has the same UID as the ABPerson) is *sometimes*
not found/not removed !
so i thought that what i would like to do is impossible because
NSMutableArray remove objects by doing sometimes :
[A isEqual:B] and sometimes [B isEqual:A] ???
i mean that sometimes a myClass instance is compared to a ABPerson
instance, so isEqual works, but sometimes the ABPerson instance is
compared to myClass, and of course the ABPerson isEqual: implementation
does not accept MyClass as equivalent object...
am i on another planet or is what i wanted to do is possible (easily
mix objects types...) ?
thanks !
_______________________________________________
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.
_______________________________________________
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.