Void pointers and userData
Hi, I'm wondering if it is possible to use the userData field of NSEvent in MacRuby. I want to add tracking rectangles to a NSView subclass, and store the object that is mouse has entered in the userData field. Something along the lines of: addTrackingRect(rect, owner:self, userData:obj, assumeInside:false) ... def mouseEntered(event) obj = event.userData[0] end However this produces the runtime error: can't convert C/Objective-C value `0x800418240' of type `ƿ_\377' to Ruby object (ArgumentError) Is it possible to set the NSEvent userData field at all in MacRuby? I can work around if not, but the userData field would be more efficient. Thanks, Kevin.
Hi Kevin, On May 2, 2009, at 6:42 AM, Kevin McGuinness wrote:
Hi,
I'm wondering if it is possible to use the userData field of NSEvent in MacRuby. I want to add tracking rectangles to a NSView subclass, and store the object that is mouse has entered in the userData field. Something along the lines of:
addTrackingRect(rect, owner:self, userData:obj, assumeInside:false)
...
def mouseEntered(event) obj = event.userData[0] end
However this produces the runtime error:
can't convert C/Objective-C value `0x800418240' of type `ƿ_\377' to Ruby object (ArgumentError)
Is it possible to set the NSEvent userData field at all in MacRuby? I can work around if not, but the userData field would be more efficient.
Thanks, Kevin.
This is a recurrent question on the list, and I unfortunately don't think it may work. You could allocate a Pointer object, set it to your object, and pass it to the addTrackingRect:owner:userData:assumeInside: method (making sure both the pointer and the object inside the pointer are retained, otherwise the GC might collect it), then later, call userData and retrieve the object inside the pointer. The problem is that userData returns ^v and there is currently no way to "cast" a Pointer object. I will add this later (RubyCocoa does it IIRC). I would recommend an alternative solution, because dealing with this can be cumbersome (and cause GC crashes if you're not careful). Laurent
participants (2)
-
Kevin McGuinness
-
Laurent Sansonetti