Archive for the ‘cocoa’ tag
Wrapping Cocoa classes in PyObjC
Yesterday I spent 2 hours trying to figure out how to wrap a single Objective-C class using the PyObjC bridge. After looking high and low and browsing through the minimal pyobjc documentation, I came up empty handed.
The closest I got was how to extract the classes from a nib, or a framework. To do that, you
import objc
objc.loadBundle(bundle_path=pathToYourBundle)
After spending another hour trying to figure out how that worked so I could wrap a single class, I gave up and emailed the pyobjc-devel list. To my surprise, I got a response within a couple of hours and was totally embarrased by the answer:
“If you’re using the PyObjC templates from Xcode, and if your objc code and python code are in the same program/project, you can get to your ObjC class(es) simply by importing Foundation in the python module — your class and methods will be found at runtime via PyObjC’s introspection mechanism. Easy!
”
In the end, the solution was to do absolutely nothing!