2012-03-17 13 views

risposta

11

Sono l'unico creatore di Python for iOS, quindi questo è ovviamente quello che vorrei raccomandare, ma un buon indicatore per la vostra decisione personale è la valutazione & di recensioni di ogni App. Mi ci sono volute settimane per capire come integrare correttamente python in Objective-C per questa applicazione, ma qui è la miglior risorsa per iniziare (tenere a mente che objC è solo un superset di C):

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


Inoltre, ecco un esempio di chiamata a una funzione definita in myModule. Il pitone equivient sarebbe:

import myModule 
pValue = myModule.doSomething() 
print pValue 

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

Per molto di più la documentazione del check-out: Extending and Embedding the Python Interpreter

+0

HI uomo grazie mille, ho ottenuto il Python per iOS, e sto amandolo, ma ho una domanda che cosa è il riferimento al legame hub git con la funzionalità nascosta ?, grazie mille !, ottimo lavoro! – MaKo

+0

https://github.com/pudquick/PythonForiOSPatches I moduli incorporati mancanti cosa fa? Ho da installare? Grazie – MaKo

+0

Ah, questo era roba che un utente ha creato per la v1.1 ma che ho implementato nella v1.2. – chown