• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
implementing isEqual: ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

implementing isEqual: ?


  • Subject: implementing isEqual: ?
  • From: Aurélien Hugelé <email@hidden>
  • Date: Fri, 14 May 2004 18:44:24 +0200

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.


  • Follow-Ups:
    • Re: implementing isEqual: ?
      • From: Vince DeMarco <email@hidden>
  • Prev by Date: Re: How to make NSImageView respond to keyboard events?
  • Next by Date: invalid operands to binary?
  • Previous by thread: Re: problems dynamically specifying services for my application
  • Next by thread: Re: implementing isEqual: ?
  • Index(es):
    • Date
    • Thread