Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Oh, looks like I misunderstood your question. Well, going the other direction isn't terribly different. This should be (as least a start of) the function you're looking for (I haven't tested it thoroughly though, so beware of the bugs):</p> <pre><code>// Returns a new reference PyObject *ObjcToPyObject(id object) { if (object == nil) { // This technically doesn't need to be an extra case, // but you may want to differentiate it for error checking return NULL; } else if ([object isKindOfClass:[NSString class]]) { return PyString_FromString([object UTF8String]); } else if ([object isKindOfClass:[NSNumber class]]) { // You could probably do some extra checking here if you need to // with the -objCType method. return PyLong_FromLong([object longValue]); } else if ([object isKindOfClass:[NSArray class]]) { // You may want to differentiate between NSArray (analagous to tuples) // and NSMutableArray (analagous to lists) here. Py_ssize_t i, len = [object count]; PyObject *list = PyList_New(len); for (i = 0; i &lt; len; ++i) { PyObject *item = ObjcToPyObject([object objectAtIndex:i]); NSCAssert(item != NULL, @"Can't add NULL item to Python List"); // Note that PyList_SetItem() "steals" the reference to the passed item. // (i.e., you do not need to release it) PyList_SetItem(list, i, item); } return list; } else if ([object isKindOfClass:[NSDictionary class]]) { PyObject *dict = PyDict_New(); for (id key in object) { PyObject *pyKey = ObjcToPyObject(key); NSCAssert(pyKey != NULL, @"Can't add NULL key to Python Dictionary"); PyObject *pyItem = ObjcToPyObject([object objectForKey:key]); NSCAssert(pyItem != NULL, @"Can't add NULL item to Python Dictionary"); PyDict_SetItem(dict, pyKey, pyItem); Py_DECREF(pyKey); Py_DECREF(pyItem); } return dict; } else { NSLog(@"ObjcToPyObject() could not convert Obj-C object to PyObject."); return NULL; } } </code></pre> <p>You may also want to take a look at the <a href="http://docs.python.org/c-api/index.html" rel="nofollow noreferrer">Python/C API Reference manual</a> if you haven't already.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload