Re: selector comparision
Ronald Oussoren <[email protected]>
| Newsgroups | gmane.comp.python.pyobjc.devel |
|---|---|
| Message-ID | <[email protected]> |
On 4 May, 2011, at 0:03, Daniel Luis dos Santos wrote: > Hello, > > I am following the apple docs on how to enable menu items according to state in my application : > The doc is at : http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MenuList/Articles/EnablingMenuItems.html%23//apple_ref/doc/uid/20000261-BAJBFGED > > I am trying to do something similar to the example code of the method > > - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem > > but in python. The class that holds the actions to the menu items has that method implemented as the docs indicate. > To avoid hardcoding the methodNames in the code I want to compare the result of the action() method to the selector of each method. My code is : > > def validateUserInterfaceItem_(self, anItem): > > theAction = anItem.action() > if (theAction == self.saveAction_): > return NO > return YES I'm not sure if this is worth the effort, you're still duplicating names. The only (small) advantage is that you don't have to use the Objective-C name. A disadvantage of your code is that 'self.saveAction_' creates a bound selector object, which means this code is less efficient that just using the selector name (although the difference is insignificant here). Anyway, the code above is broken: "theAction" is a selector name (a byte string), while self.saveAction_ is a bound selector object. You should compare to self.saveAction_.selector instead. > > Somewhere in the same class I have the method : > > @IBAction > def saveAction_(self, sender): > ... rest of method definition > > To have the selector defined for the above method I also put immediately after the method declaration : > > saveAction_ = objc.selector(saveAction_, signature="v@:@") That's not necessary, @IBAction already does this for you. > > The problem is that the comparison of the result of anItem.action() does not equals the selector for the method. > If I print both to the terminal using : > > print theAction > print self.saveAction_ > > I get : > > saveAction: > <selector saveAction: of <menubaractions: 0x1010ee000>> > > The output does not match. It seems like the representation is different. How do I compare both these values successfully ? Your comparing a selector object with a passed in selector name. To compare you should use self.saveAction_.selector, that attribute contains the selector name. Ronald ------------------------------------------------------------------------------ WhatsUp Gold - Download Free Network Management Software The most intuitive, comprehensive, and cost-effective network management toolset available today. Delivers lowest initial acquisition cost and overall TCO of any competing solution. http://p.sf.net/sfu/whatsupgold-sd _______________________________________________ Pyobjc-dev mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/pyobjc-dev
smime.p7s
(application/pkcs7-signature, 2.2 KB) - not displayed