• 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: Copying, hash, and isEqual:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Copying, hash, and isEqual:


  • Subject: Re: Copying, hash, and isEqual:
  • From: Marcel Weiher <email@hidden>
  • Date: Mon, 7 Jun 2004 09:23:58 +0100

OK. Fine... let me rephrase. "But the key is not the same!"

I hope you don't mean this seriously? Are you trying to tell me that an object is not "the same" as *itself*???

No, he's saying that an object is not the same as a copy of itself

No. (a) A copy of an object is never the same as the object itself. It is just equal. The same would be object identity. ( == ) (b) the key provided by the user for storing is the *exact* same key as the one used for retrieval. Yes, I am aware of the fact that NSDictionary makes a copy of the key, duh, that is the point of the discussion, because the copy it makes is not identical to the copy I've given it.

And so you get an implementation that calls itself a dictionary but doesn't actually work like a dictionary.

unless there is some way (e.g. -hash and -isEqual:) to tell if they are the same and that dictionary keys are copies (which is documented).

Hello?! Yes, it *is* documented. For the umpteenth time: that doesn't mean it is not a bug. It just means it is a design bug, not an implementation bug.

The bottom line is this:

- NS*Dictionary copies keys. It is documented as doing so and this is the intended behavior, by design.

YES. That is why I have said numerous times that this is a *design* bug. Goodness.

No, it's not a design bug.

Yes it is.

Go back to the very start of this thread and you'll find that the original poster complained about the incompleteness (shallowness) of the copying of the keys if they are containers.

Precisely!

If the keys are mutable then it is important that they get copied since the original might get changed after it's inserted as a key but the hash of the key is only taken at the time of insertion.

Does ANYBODY actually read what I post before shooting from the hip? At all?

- if object (A) and object (B) are to be used as a key in a dictionary, then -hash and -isEqual: need to be overridden accordingly.

Which isn't documented. In fact, the documentation for -hash recommends the *opposite*. But that's a very minor issue...

The documentation for hash says that it must return the same value for two objects if -isEqual: returns YES for that pair. The documentation for -isEqual: says that it's up to your class as to what is "equal" and you need to define it if you add instance variables.

Precisely. Equality is application-specific. NSDictionary requiring a very specific notion of equality to work around the fact that it needlessly creates a new and different object as its key. Clearer now?

NSDictionary states that it uses -hash and -isEqual: so it should be fairly obvious that these need to be properly implemented

The implementation in NSObject *is* proper. The only reason I created a subclass is that NSObject doesn't implement NSCopying, which NSDictionary unreasonable expects to be present.

in a class if your going to use instances of that class as keys. If copies of objects are to be deemed equivalent that you need to implement these messages.

What if I deem them equivalent exactly if they are the same, just as NSObject does?


Your implementation does not follow the second rule. Your key is not the *very same identical* key as you claim.

Of course it is!

The copy of the object is not the same as the object.

Yes. But that is not *my* key. That is the *new* key that NSDictionary makes.

The base implementation of NSObject uses the object address in the -hash and -isEqual: implementations but it also does not implement -copyWithZone: so there is no way to get a copy of an existing NSObject (or subclass), so you can't assume that the base implementations are useful for copies of your subclass. If you implement copying then you need to make a discussion as to the equivalence of the original and the result of copying.

Saying that my objects are equal only if they are identical is a legitimate choice.


@interface MyObject : NSObject <NSCopying>
{}
@end
@implementation MyObject
-copyWithZone:(NSZone*)zone
{
id copy=[[[self class] alloc] init];
return copy;
}
@end

Incidentally, given your object was immutable (implicitly, since is had no instance variables) it would have been legitimate to simply return self in this -copyWithZone: (which is what happens with immutable strings).

Yes, but that is an optimization...

Marcel


--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
1d480c25f397c4786386135f8e8938e4
_______________________________________________
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: Copying, hash, and isEqual:
      • From: Marcel Weiher <email@hidden>
References: 
 >Ugly bug in Foundation, beware! (From: Ondra Cada <email@hidden>)
 >Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Alastair Houghton <email@hidden>)
 >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Ondra Cada <email@hidden>)
 >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Alastair Houghton <email@hidden>)
 >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Brent Gulanowski <email@hidden>)
 >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Alastair Houghton <email@hidden>)
 >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Ondra Cada <email@hidden>)
 >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Marcel Weiher <email@hidden>)
 >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Brent Gulanowski <email@hidden>)
 >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: "Louis C. Sacha" <email@hidden>)
 >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Marcel Weiher <email@hidden>)
 >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Nat! <email@hidden>)
 >Re: NSDictionary design bug (was: Re: Ugly bug in Foundation, beware!) (From: Marcel Weiher <email@hidden>)
 >Copying, hash, and isEqual: (From: "b.bum" <email@hidden>)
 >Re: Copying, hash, and isEqual: (From: Marcel Weiher <email@hidden>)
 >Re: Copying, hash, and isEqual: (From: "b.bum" <email@hidden>)
 >Re: Copying, hash, and isEqual: (From: Marcel Weiher <email@hidden>)
 >Re: Copying, hash, and isEqual: (From: Nicko van Someren <email@hidden>)

  • Prev by Date: Radio Buttons and Binding
  • Next by Date: Re: Copying, hash, and isEqual:
  • Previous by thread: Re: Copying, hash, and isEqual:
  • Next by thread: Re: Copying, hash, and isEqual:
  • Index(es):
    • Date
    • Thread