Re: [Pyobjc-dev] always replace colons with underscores... most of the time
Brought to you by:
ronaldoussoren
|
From: Bill B. <bb...@ma...> - 2007-11-12 01:46:06
|
On Nov 11, 2007, at 1:23 PM, Chris McClimans wrote:
> class Person(NSObject):
> personName = objc.ivar()
> expectedRaise = objc.ivar.float()
> def setExpectedRaise_(self, expectedRaise):
> self.expectedRaise = expectedRaise
> def setPersonName_(self, personName):
> self.personName = personName
>
> It looks like it should work fine for KVC, however the trailing
> underscores on the setAttribute_ calls are not valid. If you replace
> the colons in the objective-c calls with underscores in python as I
> have seen else where, you will get an error when using KVC via an
> NSArrayController.
>
> An unexpected error occured: (AttributeError: 'Person' object has no
> attribute 'setExpectedRaise')
>
> This is resolved when removing the trailing underscore from the method
> call.
>
> Is this an exception to the replace colons in objective-c with
> underscores in python rule?
The above code looks slightly unexpected. Namely, it doesn't
implement the -expectedRaise and -personName instance methods.
Try implementing expectedRaise() and personName() methods (moving the
iVars to _personName and _expectedRaise) most likely.
It is likely that NSArrayController is confused by the lack of the
expected method pair.
Note that it is NSAC that is tripping over this, not KVC:
>>> from Foundation import *
>>> from objc import *
>>> class Person(NSObject):
... personName = objc.ivar()
... expectedRaise = objc.ivar.float()
... def setExpectedRaise_(self, expectedRaise):
... self.expectedRaise = expectedRaise
... def setPersonName_(self, personName):
... self.personName = personName
...
>>> x = Person.new()
>>> x.valueForKey_("expectedRaise")
0.0
>>> x.setValue_forKey_(10.0, "expectedRaise")
>>> x.valueForKey_("expectedRaise")
10.0
>>> x.expectedRaise
10.0
>>> x.setExpectedRaise_
<selector setExpectedRaise: of <Person: 0x1ae7490>>
(stock Leopard)
b.bum
|