codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Equivalent Objective C syntax in python using pyobjc


By : , Category : python

Python doesn't deal with pointers at all; in fact, it actively discourages their use in all ways. You can use the id() function to retrieve the address of a Python object, but there isn't a corresponding function to go the other way (that isn't a joke).

Now, given that you have a pointer to something from C, you should be able to describe that something with the ctypes module and then treat the address as a pointer and decode at will. But it is tricky.

Not the most concrete of examples, but the Venn diagram of people that know Python, Objective-C, and are familiar with ctypes is a rather small group (which is one big reason why you are generally better off sticking to native ObjC for iOS/Cocoa).

ReLated :

Turns out I was missing an underscore, it works when you do this:

OBJC.function_with_param_('a')

Found it here: http://lists.stat.ucla.edu/pipermail/computing/attachments/20050825/f9a98389/attachment.pdf

The easiest way to ensure problem free usage of the bridge is to ensure that the delegate methods that you use don't use C arrays as arguments or return values and don't use variadic signatures ("..."). Futhermore ensure that all pass-by-reference arguments (such as the commonly used "NSError**" argument) are marked up with "in", "out" or "inout" to indicate in which direction values are passed. This ensures that the bridge can get all information it needs from the Objective-C runtime.

There are two options to pass a preconstructed object to Python code:

  1. Create a class method that returns the object

  2. Use the PyObjC API to create the python proxy for the Objective-C object.

The latter uses an internal PyObjC API (also used by the framework wrappers) and could break in future versions of PyObjC. That said, I don't have active plans to break the solution I describe here.

First ensure that the right version of "pyobjc-api.h" and "pyobjc-compat.h" are available for the Objective-C compiler.

Use #include "pyobjc-api.h" to make the API available.

Call "PyObjC_ImportAPI" after initialising the Python interpreter, but before you use any other PyObjC function.

Use "pyValue = PyObjC_IdToPython(objcValue)" to create a Python representation for an Objective-C object.

NSArray *subsetArray = [originalArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)]];

You can change the starting point and length inside of the NSMakeRange().

The closest equivalent is

mystring = "iBeacon major:%s minor:%s" % (majorVersion, minorVersion)

However, the recommended way to do it with Python 2.7+ is to use str.format instead of the % method:

mystring = "iBeacon major:{} minor:{}".format(majorVersion, minorVersion)
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .