Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to add and call Python code from an existing iOS project written in Swift

I need a way to call python code from swift, a library would be ideal. I've done a considerable amount of google searching, and the closest material I found is for objective-c. (posted by Shaun)

5 Answers

If anyone is ever interested in calling python from swift, here is some helpful material I found(used) -

Most of it is for Objective-c, but if you need to use swift you can easily just create an ObjC-Swift bridge (super-super easy) - Lookup the apple docs

I managed to get the first two methods working on an OS X app, but I got some architecture errors for iOS(not important). I've pushed the project aside for now, I have a ton I need to get done.

Hope this will be helpful for anyone interested :-)

-Shaun

Please let me know if it works I would be curious to hear how it plays out.

Are you trying to write python code in a Swift file?

No, I have an engine written in python that took me a while to build. Instead of rewriting the whole thing in swift, I just want to make method calls from swift to the python engine.

Sorry, I don't know of any way to do that.

I don't know that you can given Python and Swift are not completely compatible. I wonder if you could create a wrapper of sorts? I know you could import C++ into a Swift file if you create an objective-C wrapper for it, but than again those are C based languages.

Thanks for the hint Tyler. I'm going to try to create a C wrapper and see if that works.