Re: Passing numbers through userdata
Re: Passing numbers through userdata
- Subject: Re: Passing numbers through userdata
- From: Fritz Anderson <email@hidden>
- Date: Wed, 15 Dec 2004 08:54:15 -0600
On 14 Dec 2004, at 10:56 PM, Christopher Drum wrote:
I'm running into a small issue with passing an NSNumber through the
userdata property of a tracking rect. When adding the tracking rects,
I put an int into an NSNumber and put that into the userdata. Then, I
grab [[theEvent userData] intValue] in -(void)mouseEntered:(NSEvent
*)theEvent. The compiler flags a warning of "invalid receiver type
'void *' on that particular line of code. However, it works. So, even
though it's flagging the code, it's clearly not a big problem... or is
it?
-[NSEvent userData] is declared to return a void pointer. There is no
guarantee to the compiler, within your mouseEntered: handler, that the
userData pointer is in fact a pointer to an Objective-C object. You,
however, do know it is an NSNumber, so you can reassure the compiler by
casting the void pointer:
[(NSNumber *) [theEvent userData] intValue];
If I pass an int directly through the userdata property, I get the
problem "warning: passing arg 3 of
`addTrackingRect:owner:userData:assumeInside:' makes pointer from
integer without a cast" However, it does work, even though when I
retrieve the int I get the warning, "assignment makes integer from
pointer without a cast"
You could cast the integer to void * (as in userData: (void *) myInt),
but it is bad karma to use pointer storage for integers (and vice
versa, as when you do the reverse cast at the other end). C does not
guarantee that the round-trip cast
(int) (void *) anInt
will produce the same value as anInt.
-- F
--
Fritz Anderson
Consulting Programmer Chicago, Illinois
http://resume.manoverboard.org/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden