Re: [Pyobjc-dev] Key Value Observing of lists?
Brought to you by:
ronaldoussoren
|
From: Ronald O. <ron...@ma...> - 2008-06-02 05:38:26
|
On 1 Jun, 2008, at 21:43, Orestis Markou wrote: > Hi, > > I've just stumbled on this again (I'll be writing a short blog post > about it when my host is back online). I think I'm a bit confused... > >>> Hi, >>> >>> I'm trying to bound an NSTableView to a list using an array >>> controller. This works fine, as long as I don't mutate the list >>> (list.append, for example). >> >> I'm pretty sure this bit is documented somewhere. It is >> technically impossible to make PyObjC do the right thing here >> without patching the Python interpreter. > > Just to make things very clear, is this not possible even if using a > Cocoa collection like NSMutableArray and calling Cocoa methods on it > (insertObject_atIndex_)? No. It is impossible to make PyObjC do the right thing for "pure" Python types, like list, dict, and object. The usual magic works just fine for subclasses of NSObject. It works even better than in ObjC: NSObject.__setattr__ will do the KVO dance for you, so you can code like this and still use KVO: class MyClass (NSObject): def doSomething(self): self.foo = '42' self.bar = 1.2 When you call doSomething PyObjC will automaticly emit willChange/ didChange events for the keys 'foo' and 'bar'. > > > So basically, _every_ method that mutates something that needs to be > observable must be surround with "willChangeValueForKey_" and > "didChangeValueForKey_", right? Only when you mutate an object without a native Objective-C representation, because it is impossible to intercept all mutations of a Python object without cooperation of that object. > > > Is there any shortcut for doing this? Use NSMutableArray instead of list, and likewise for other datastructures. Ronald > > > Regards, > Orestis |