views:

77

answers:

1

This question is basically the inverse of this other question: http://stackoverflow.com/questions/1308079/calling-python-from-objective-c

I have implemented my iPhone application logic in Objective-C (obviously), and am now trying to re-use as much as possible from my XCode project in the server component to save on double-implementation. I have successfully loaded the CoreData data model from Python, however, can't see a way to actually call into the Objective-C logic from Python.

Basically I'm trying to access the Objective-C classes and methods in my iPhone project from Python to save myself duping out all the implementations.

Is this even vaguely possible, or is dupe-implementation the only solution here? Seems like the kind of thing Boost::Python might be used for, but I'm not really sure.

edit: Boost::Python won't work because it is C++ based and I need Objective-C. I knew there was a reason why that didn't work.

+1  A: 

If your Objective-C code is in a framework and you would like to essentially write a Python application that uses your framework, then you can use objc.loadBundle, and then use objc.lookUpClass or NSClassFromString to get access to your classes. From there, you can use your classes like any other bridged Objective-C class.

If you're running your Python code within a process that already has the Objective-C runtime up, and your classes are already registered with it, then you can skip the loadBundle step.

Joseph Spiros
Thanks for the reply Joseph. I think I have to manually load the bundle since the iPhone project is completely separate from the pyobjc server except for the .momd data model defn that I manually copied over.Sorry for the noob question, but where would I find the bundle that is built by XCode for the iPhone project? I looked in my_project/build and there is some stuff in there that looks like it has been compiled for iOS rather than OSX, so not sure if that will work if I try to load it on the server (which is running on OSX, obviously).
glenc
You will need to create a new build target that builds a Mac OS X bundle or framework using code that will compile on the Mac. If you need help with this, you should ask another question, as that's more Xcode specific than anything else.
Joseph Spiros
Thanks for your help Joseph, I'll take up the XCode issue in a new question
glenc