..
print urllib.urlopen("http://google.com").read()
- Python ekleyin. projenize çerçeve (Sağ
External Frameworks..
, Add > Existing Frameworks
tıklayın. çerçeveyi içinde /System/Library/Frameworks/
- içinde "Başlık Arama Yolu"
/System/Library/Frameworks/Python.framework/Headers
ekle (Project > Edit Project Settings
)
Aşağıdaki kod çalışması gerekir
(muhtemelen şimdiye kadar yazılmış en iyi kod olmasa da ..)
#include <Python.h>
int main(){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Py_Initialize();
// import urllib
PyObject *mymodule = PyImport_Import(PyString_FromString("urllib"));
// thefunc = urllib.urlopen
PyObject *thefunc = PyObject_GetAttrString(mymodule, "urlopen");
// if callable(thefunc):
if(thefunc && PyCallable_Check(thefunc)){
// theargs =()
PyObject *theargs = PyTuple_New(1);
// theargs[0] = "http://google.com"
PyTuple_SetItem(theargs, 0, PyString_FromString("http://google.com"));
// f = thefunc.__call__(*theargs)
PyObject *f = PyObject_CallObject(thefunc, theargs);
// read = f.read
PyObject *read = PyObject_GetAttrString(f, "read");
// result = read.__call__()
PyObject *result = PyObject_CallObject(read, NULL);
if(result != NULL){
// print result
printf("Result of call: %s", PyString_AsString(result));
}
}
[pool release];
}
Ayrıca this tutorial iyidir
Yinelenen: http://stackoverflow.com/questions/49137/calling -python-den-ac-program için dağılımının; http://stackoverflow.com/questions/297112/how-do-i-use-python-libraries-in-c. Python'u herhangi bir uygulamaya gömebilirsiniz. –
@ S.Lott Bu bir kopya değil; Bu sorular C++, Objective-C ile ilgili değildir. C++ ve Objective-C'yi karıştırmak için Objective-C++ özelliğini kullanabilmeniz için Objective-C sınıfları olarak kullanmanız gerekiyorsa, Object-C sınıflarındaki tüm C++ kodunu kendiniz sarmanız gerekir. –