Re: [Pyobjc-dev] Trouble with respondsToSelector and performSelector
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2005-04-01 04:00:18
|
On Mar 31, 2005, at 9:41 PM, Scott Johnston wrote: > I have a controller which receives mouse activity from a TableView. > The controller sets a mouse "target" and mouse "actions" in the > tableview. > > This code, added to my variation of DNDTableView, works fine, but is > "pythonic": > > def mouseDown_(self, event): > if self._mousetarget is not None and > hasattr(self._mousetarget,self._mousedownaction): > getattr(self._mousetarget,self._mousedownaction)(event) > else: > super(DNDTableView, self).mouseDown_(event) > > When I try to translate the code into "Cocoa", the app segfaults > shortly after execution: > > def mouseDown_(self, event): > if self._mousetarget is not None and > self._mousetarget.respondsToSelector_(self._mousedownaction): > > self._mousetarget.performSelector_withObject_(self._mousedownaction, > event) > else: > super(DNDTableView, self).mouseDown_(event) > > As my code works, this is not an emergency, but I'd like to know just > how deep my misunderstanding of Cocoa is. (Code for Dragged and Up is > similar to the above). > > Running pyobjc1.3b1 under Panther. performSelector:withObject: only works for selectors that have a certain signature, this one probably doesn't. The answer is: don't do that. It's hard to say without knowing what the value of "_mousedownaction" is, and what the implementation of that selector looks like. respondsToSelector: should work fine, though. There is no real benefit to "ObjC-izing" a PyObjC app.. Personally, I would use the respondsToSelector:, but not attempt to use any of the performSelector_blah methods unless it was to defer execution or run something on the main thread. It's generally not a good idea to use ivars that start with just an underscore because those are reserved by Apple and can interfere with KVC if you have a similarly named accessor. -bob |