• 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
Re: Wisdom of overriding isEqual:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Wisdom of overriding isEqual:


  • Subject: Re: Wisdom of overriding isEqual:
  • From: Glen Low <email@hidden>
  • Date: Thu, 11 Mar 2004 06:25:37 +0800

On 10/03/2004, at 10:48 PM, email@hidden wrote:

Lately, I've had the need to store some insances of NSURLRequest in an
NSSet, which have to be unique with respect to their contents. So I have
overriden the isEqual: message on NSURLRequest using a category. Now NSSet
(calling isEqual:) will examine the attributes of NSURLRequests to determine
if they are equal. This works fine.

My question is: won't this trip up any of the internal workings of Cocoa?
Suppose 'somewhere insyde' Cocoa relies on the default implementation of
isEqual:?

Pat:

You will need to override hash as well. Ensure that:

[a isEqual: b] implies [a hash] == [b hash]

and

[a hash] == [b hash] mostly implies [a isEqual: b]

NSSet, NSDictionary use hash to efficiently index stored objects. Given a key, it gets the hash, looks in the internal bucket corresponding to the hash, then runs isEqual exhaustively in all the objects in the bucket (more or less). If it didn't use hash, it would have to run isEqual on all the elements, which would be slow.

I'm not sure of the merits of overriding methods in a category, since that might disturb how NSURLRequests are processed by other parts of the system. Why not subclass NSURLRequest and override both isEqual and hash there?

Cheers, Glen Low


---
pixelglow software | simply brilliant stuff
www.pixelglow.com
_______________________________________________
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.


  • Prev by Date: Re: [SOLVED...but] Re: Storing NaN in NSMutableArray ?
  • Next by Date: Re: Advice needed about a possible bug in NSMethodSignature or NSInvocation
  • Previous by thread: Re: Wisdom of overriding isEqual:
  • Next by thread: Re: Wisdom of overriding isEqual:
  • Index(es):
    • Date
    • Thread