• 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: Passing numbers through userdata
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Passing numbers through userdata
      • From: Larry Fransson <email@hidden>
References: 
 >Passing numbers through userdata (From: Christopher Drum <email@hidden>)

  • Prev by Date: Re: Kill other process's warning dialog
  • Next by Date: Re: NSBrowser problems
  • Previous by thread: Passing numbers through userdata
  • Next by thread: Re: Passing numbers through userdata
  • Index(es):
    • Date
    • Thread