• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.9.5/pyobjc-42/2.5/pyobjc/pyobjc-framework-Cocoa/Examples/Foundation/Scripts/
1#!/usr/bin/python
2
3import objc
4from Foundation import NSObject, NSKeyValueObservingOptionNew, NSKeyValueChangeNewKey
5
6class MyClass(NSObject):
7    base = objc.ivar("base", objc._C_INT)
8    power = objc.ivar("power", objc._C_INT)
9    result = objc.ivar("result", objc._C_INT)
10
11    def result(self):
12        return self.base ** self.power
13
14MyClass.setKeys_triggerChangeNotificationsForDependentKey_(
15    [u"base", u"power"],
16    u"result"
17)
18
19class Observer(NSObject):
20    def observeValueForKeyPath_ofObject_change_context_(self, path, object, changeDescription, context):
21        print 'path "%s" was changed to "%s".' % (path, changeDescription[NSKeyValueChangeNewKey])
22
23myInstance = MyClass.new()
24observer = Observer.new()
25
26myInstance.addObserver_forKeyPath_options_context_(observer, "result", NSKeyValueObservingOptionNew, 0)
27myInstance.addObserver_forKeyPath_options_context_(observer, "base", NSKeyValueObservingOptionNew, 0)
28myInstance.addObserver_forKeyPath_options_context_(observer, "power", NSKeyValueObservingOptionNew, 0)
29
30myInstance.setValue_forKey_(2, "base")
31myInstance.power = 4
32
33print "%d ** %d == %d" % (myInstance.base, myInstance.valueForKey_("power"), myInstance.result())
34
35
36