Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Is there an equivalent to objc_util, but for Swift?
-
Just curious, since I don't fully understand the details beyond using some kind of CFFI to do this kind of magic: is there any possibility of creating a Swift bridge for pythonista, similar to how we currently use objc_util or rubicon to call out to objective-c?
Or does such a thing already exist that we can use? I see some projects for python which talk about swift/python interoperability, but it isn't clear to me whether they are things we could use in pythonista.
Or...is such a thing completely impossible given how Swift differs from objective-c?
-
Never used Swift but a quick search came up with https://github.com/mojidabckuu/SBridge
-
@shinyformica, are there some device features available only via Swift?
-
@stephen not sure what SBridge is, exactly? Doesn't seem python related?
@mikael it just came up at work in recent discussions about the future of using pythonista to do rapid development. People were wondering if there was any route to swift that was as simple as it is for objective-c.
Not that Apple is planning to stop supporting objective-c in the near future, but more people are comfortable swift or with learning swift, than with picking up objective-c.
Most of what I see when googling is about calling/embedding python from swift, not the other way around (calling swift from python). There's this:
https://gist.github.com/jiaaro/e111f0f64d0cdb8aca38
But that is not something which can be integrated with pythonista.
-
Sbridgebis Objc bridge (objc_utils) for Swift as requested by Post Title."Is there an equivalent to objc_util, but for Swift?"was this not what you are requesting my freind?EDIT
I see what you are requesting now. i appoligize.
with a quick study i see that you can use python in Swift and when combined, swift and objc you can use swift inside python. possibly look into
python_interoperability
possibly?might have to combine objc, python3 and swift as long as your mainly passing data between them. and not "cooprative" classes. in that case
Sbridge
would actually come in handy.my best guess. sorry again for the miss understanding 😊
-
@shinyformica, very interesting. I have wondered about the rapid prototyping aspect myself.
My first reaction is that you do not need to learn ObjC to use the necessary iOS capabilities in Pythonista. Just some syntax conversion tricks, and then look up the manual.
But I get that the intent is more to lower the barrier of entry.
In terms of using actual custom Swift code as part of your non-App Store-deployed Pythonista app, I guess we are out of luck.
And going via ObjC looks like a no go without including some generated header files.
@omz, @jonb, @dgelessus, any expert opinions?
-
I'm also not aware of any existing project that allows calling Swift from Python. As others have said above, the other direction (calling Python from Swift) is quite well-supported, but that doesn't help us much here.
In principle, it is entirely possible to call Swift from Python. It would require more work than with Objective-C though.
Objective-C's calling conventions are completely compatible with C, which allows using the standard
ctypes
library to call Objective-C code. The Objective-C API also exposes a lot of information at runtime, which allows inferring method return/argument types so you don't have to set them manually in most cases.With Swift the situation is quite different. As far as I know, Swift itself does not provide nearly as many runtime API functions as Objective-C does, so there is no easy way to get function/method type information at runtime. Swift might not even store this type information in the compiled binaries at all (I haven't checked recently).
There's also the more fundamental problem that Swift's type system and calling convention are completely different from C. In some simple cases the calling conventions overlap enough that you can call some Swift functions using
ctypes
, but IIRC all instance methods (anything with aself
) and functions/methods with certain complex return types (tuples, etc.) are incompatible with the C calling convention.This means that calling Swift from Python would require a custom extension module similar to
ctypes
, but for the Swift calling convention. Internally,ctypes
uses the libffi library, but libffi does not support the Swift ABI (yet?). In any case, since a custom native module is required, a library like this wouldn't be usable in Pythonista unless @omz compiles and includes it in the app.TLDR: It would be possible, but would require quite a bit of work, and couldn't be developed in Pythonista.
-
Is the idea just to use pythonista for the UI bits, and write Swift backends?
Or vice versa?
Or just to try out some Swift libraries?
Swift playgrounds seems to be a place one can play in Swift for rapid prototypes on the iPad.