[Pyobjc-dev] property progress
Brought to you by:
ronaldoussoren
From: Ronald O. <ron...@ma...> - 2010-01-21 15:38:45
|
This now works on my machine: import objc NSObject = objc.lookUpClass('NSObject') NSKeyValueObservingOptionNew = 0x01 NSKeyValueObservingOptionOld = 0x02 class Observer (NSObject): def init(self): self = super(Observer, self).init() self.keys = [] return self def dealloc(self): for o, k in self.keys: o.removeObserver_forKeyPath_(self, k) def observeValueForKeyPath_ofObject_change_context_(self, path, object, change, context): print "===> %s\t\t%s"%(path, change) def register(self, object, keypath): object.addObserver_forKeyPath_options_context_( self, keypath, NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld, 0) self.keys.append((object, keypath)) class Person (NSObject): def _init(self): self = objc.super(Person, self).init() self.firstName = None self.lastName = None return self firstName = objc.object_property() lastName = objc.object_property() fullName = objc.object_property(read_only=True, ivar=objc.NULL) fullName.depends_on(u'firstName') fullName.depends_on(u'lastName') @fullName.getter def fullName(self): return "%s %s"%(self.firstName, self.lastName) obs = Observer.alloc().init() pers = Person.alloc().init() obs.register(pers, "fullName") obs.register(pers, "firstName") obs.register(pers, "lastName") pers.firstName = "Ronald" pers.lastName = "Oussoren" print "done" print pers.fullName That is, I can define something that behaves like a python property for Python code, but is implemented using ObjC-style getter/setter methods and therefore participates properly in key-value observations. What makes me really happy is that adding some hooks to the bridge allowed me to write class objc_property in Python, which should make it a lot more convenient to add simular properties that represent an array or set. I haven't committed this to the repository yet, I have to add unittests and documentation before I do that. Ronald |