2012-03-17 68 views

回答

11

我的Python for iOS唯一的创建者,这样当然是什么,我会推荐的,但是对于你的个人决定一个很好的指标是检查各应用程序的&评级。我花了几周弄清楚如何蟒蛇正确融入的Objective-C为这个应用程序,但这里是让你开始的最佳资源(记住,ObjC只是一个C的超集):

http://docs.python.org/c-api/


此外,这里是一个调用在myModule中定义的函数的示例。该equivient蟒蛇是:

import myModule 
pValue = myModule.doSomething() 
print pValue 

在Objective-C:

#include <Python.h> 

- (void)example { 

    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue; 
    NSString *nsString; 

    // Initialize the Python Interpreter 
    Py_Initialize(); 

    // Build the name object 
    pName = PyString_FromString("myModule"); 

    // Load the module object 
    pModule = PyImport_Import(pName); 

    // pDict is a borrowed reference 
    pDict = PyModule_GetDict(pModule); 

    // pFunc is also a borrowed reference 
    pFunc = PyDict_GetItemString(pDict, "doSomething"); 

    if (PyCallable_Check(pFunc)) { 
     pValue = PyObject_CallObject(pFunc, NULL); 
     if (pValue != NULL) { 
      if (PyObject_IsInstance(pValue, (PyObject *)&PyUnicode_Type)) { 
        nsString = [NSString stringWithCharacters:((PyUnicodeObject *)pValue)->str length:((PyUnicodeObject *) pValue)->length]; 
      } else if (PyObject_IsInstance(pValue, (PyObject *)&PyBytes_Type)) { 
        nsString = [NSString stringWithUTF8String:((PyBytesObject *)pValue)->ob_sval]; 
      } else { 
        /* Handle a return value that is neither a PyUnicode_Type nor a PyBytes_Type */ 
      } 
      Py_XDECREF(pValue); 
     } else { 
      PyErr_Print(); 
     } 
    } else { 
     PyErr_Print(); 
    } 

    // Clean up 
    Py_XDECREF(pModule); 
    Py_XDECREF(pName); 

    // Finish the Python Interpreter 
    Py_Finalize(); 

    NSLog(@"%@", nsString); 
} 

对于更多的文档退房:Extending and Embedding the Python Interpreter

+0

HI人非常感谢,我得到了Python的iOS应用,和我爱它,但我有一个问题,什么是参考与这些混帐枢纽链接隐藏的功能?,非常感谢!,真棒! – MaKo 2012-03-17 21:11:13

+0

https://github.com/pudquick/PythonForiOSPatches缺少的内置模块这是做什么的?何安装?谢谢 – MaKo 2012-03-17 21:41:24

+0

啊,这是用户为v1.1创建的东西,但是我在v1.2中实现了。 – chown 2012-03-17 23:22:59