Re: Storing values in dictionary with their address as the key
Re: Storing values in dictionary with their address as the key
- Subject: Re: Storing values in dictionary with their address as the key
- From: "Carter R. Harrison" <email@hidden>
- Date: Mon, 28 Jul 2008 15:32:24 -0400
On Jul 28, 2008, at 3:24 PM, David Wilson wrote:
On Mon, Jul 28, 2008 at 2:13 PM, Carter R. Harrison
<email@hidden> wrote:
Hey Everybody,
If I wanted to store an object in a dictionary and set its key as the
object's memory address - how would I go about doing this?
Right now I'm doing this:
int i;
for (i = 0 ; i < 10 ; i++)
{
NSObject *myObject = [[NSObject alloc] init];
[dictionary setValue:myObject forKey:[NSString
stringWithFormat:@"%x",
&myObject]];
}
Use:
int i;
for (i=0;i<10;i++)
{
NSObject *myObject = [[NSObject alloc] init];
NSValue *val = [NSValue valueWithPointer:myObject];
[dictionary setValue:myObject forKey:val];
}
Or [NSNumber numberWithInt:(int)myObject] instead of the NSValue.
Interesting.. thanks for the solution. Naturally almost immediately
after I sent my original question to the list I figured out what was
wrong with my solution. The issue was with the format string..
Instead of a %x, I needed a %qx. The %qx displays a 64 bit address
whereas the %x displays a 32 bit address. When you give %x, only the
least significant 32 bits are printed and those happen to always be
the same (at least in my case).
--
- David T. Wilson
email@hidden
_______________________________________________
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