Calling custom Objective-C from a pyobjc application?

 ̄綄美尐妖づ 提交于 2019-12-08 09:23:34

问题


This question is basically the inverse of this other question: 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:


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.



来源:https://stackoverflow.com/questions/3180574/calling-custom-objective-c-from-a-pyobjc-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!