Re: Integer as key in NSMutableDictionary
Re: Integer as key in NSMutableDictionary
- Subject: Re: Integer as key in NSMutableDictionary
- From: Greg Parker <email@hidden>
- Date: Mon, 4 May 2009 23:24:32 -0700
That's right. In some discussions of object-oriented programming, a
distinction is made between "value objects" and "reference objects".
Two value objects can be "equal" if they share the same "value", even
if they are separately allocated objects with different pointer
addresses in memory. For reference objects, "equal" means they have
the same pointer address.
NSNumber acts as a value object by this terminology, because
NSNumber's -isEqual: and -hash methods operate on the represented
number value, ignoring the actual allocation addresses. On the other
hand, NSWindow is a reference object; nobody would consider two
different NSWindow objects "equal" even if they shared the same name
and contents and position on screen.
NSDictionary uses -isEqual: and -hash everywhere, so the contained
objects get to decide whether they act as value objects or reference
objects. NSWindow's -isEqual: implementation simply compares the
pointer addresses and returns that result, while NSNumber's -isEqual:
implementation also looks at the represented number values stored the
objects.
A CFDictionary can be configured to treat all of its objects as
reference objects, ignoring any -isEqual: implementations. (Such a
dictionary would no longer be compatible with NSDictionary, though.)
NSMapTable and NSHashTable can be configured similarly, using NS[Map|
Hash]TableObjectPointerPersonality.
Finally, note that calling -numberWithInt: multiple times with the
same value may in fact give you the same object pointer back. That is,
`[NSNumber numberWithInt:1] == [NSNumber numberWithInt:1]` may be
true. But don't rely on that - it's an internal optimization, doesn't
apply to all values, and changes in different OS versions.
On May 4, 2009, at 10:58 PM, Jonathan Hess wrote:
Hey Weydson -
NSDictionary equates keys by using -[NSObject isEqual:] and -
[NSObject hash], so a number with a different pointer address but
the same value as determined by isEqual: is fine.
If identity vs value does become an important distinction for you,
CFDictionary gives you control over that.
Good Luck -
Jon Hess
On May 4, 2009, at 10:52 PM, Weydson Lima wrote:
Thanks all for the replies. I clearly understand now that I should
use
NSNumber instead of NSInteger. However, another question came up:
when I use
the removeObjectForKey, how can I make a reference to a specific
key? Let's
say I want to remove key that was initialized with [NSNumber
numberWithInt:1]. If I call removeObjectForKey:[NSNumber
numberWithInt:1],
that method won't work because a new NSNumber pointer is being
created,
correct?The only solution I could find is to use enumeration and go
through
each key and gets its integer value.
_______________________________________________
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