Re: NSDictionary trouble
Re: NSDictionary trouble
- Subject: Re: NSDictionary trouble
- From: Jeremy Pereira <email@hidden>
- Date: Tue, 19 Jan 2010 21:33:57 +0000
On 19 Jan 2010, at 16:53, Shawn Rutledge wrote:
> I'm accustomed to
> things like Qt and Java where hashtables can contain anything for both
> key and value.
This is not true (at least for Java and probably for QT for the same reason). From the API docs for the Map abstract class:
"Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. "
From the docs for Object.hashCode()
"Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified."
The Cocoa "restriction" on keys actually makes things a little easier. For instance, you can use NSMutableString as a key in an NSDictionary with no problems. However, if you try to use a StringBuffer or StringBuilder as a key in a Java map, you won't get the "natural" behaviour - two distinct StringBuffers with the same character sequence in them do not compare equal and if you override equals() in a subclass to make sure they do, you'd better not change a StringBuffer that happens to be a key in a map._______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden