2012-03-17 38 views

Respuesta

11

Soy el único creador de Python for iOS por lo que es, por supuesto, lo que recomendaría, pero un buen indicador para su decisión personal es las evaluaciones & calificaciones de cada aplicación. Me tomó semanas para encontrar la manera de integrar adecuadamente pitón en Objective-C para esta aplicación, pero aquí es el mejor recurso para que pueda empezar (tener en cuenta que ObjC es sólo un superconjunto de C):

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


Además, aquí hay un ejemplo de llamar a una función definida en myModule. La pitón equivient sería:

import myModule 
pValue = myModule.doSomething() 
print pValue 

En 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); 
} 

Para más cheque documentación a cabo: Extending and Embedding the Python Interpreter

+0

hombre HI Muchas gracias, me dio la Python para iOS, y soy amante de ella, pero tengo una pregunta ¿cuál es la referencia al enlace del concentrador git con la funcionalidad oculta ?, muchas gracias !, excelente trabajo! – MaKo

+0

https://github.com/pudquick/PythonForiOSPatches Los módulos integrados faltantes ¿qué hace esto? Ho para instalar? Gracias – MaKo

+0

Ah, esto era algo que un usuario creó para v1.1 pero lo implementé en v1.2. – chown

Cuestiones relacionadas