Does openFrameworks Swift (new Apple's programming language) compatible?

I have no clue of how — and had yet tried anything, to be honest.
I saw Apple said that it sit along side obj-c just fine. So my first question in mind is would that also apply to C++?

I’m wondering if anyone had a chance to try it at all?


“You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code.”

Swift was just announced 2 days ago. So noway its openframeworks compatible. Have not tried it yet, But looking at the documentation the only way to use c++ is to wrap into C first, then have swift call the C function. But right now C compatibility with Swift is buggy. I heard on the people are experiences lot of crashes trying to use Core Audio with Swift.

Once the official release comes out I’ll definitely get openframeworks working with Swift.
We should remove all objective c that we are currently using and replace it with either swift or pure C. This means classes like AVPlayer and avSoundPlayer need to be replaced with the lower level C apis like Core Video and openAL.

Apple might also improve c++ support in the future.

Hi! I’d love to see OF and swift play nicely- have you had an opportunity to tinker with it at all? Thanks!

With version 2 it seems easier.

Hmm, I don’t believe the C++ interop story has changed with Swift 2.0, so one must still shim / trampoline via Objective-C or C.

EDIT: Linking another thread about Swift 2.0:

Swift seems to get open sourced some time in the future (fairly soon, I believe), so I think with some dedication we’ll find a way to work it out. Though, having some experience with it I’m not sure if it’s gonna be that cool anyway. Personally I still prefer C++11 with oF for graphic stuff, it suits me better than Swift+Cocoa. Maybe it’ll change with more experience, yet I’ve already stumbled upon some clever critique of the Swift language design, and I tend to agree with at least some of it. Well, it’s still evolving…

Speaking about the pure OSX/iOS option of writing Swift on top of the C++ API, it’s interesting to know whether it’s possible or not right now. I believe someone can get the basic stuff up and running, but the further you’ll go the more the nature of API will oppose the Swift ways (collection types, optionals, it can go on and on). Keep in mind that it wasn’t easy even for Apple itself to make Swift work well with Objective C code with their own APIs. My guess is that the more low-level you go, the harder it’ll be to make it work.

But it looks quite possible on a surface to make it work for some simple high-level apps. We just need a way to expose our C++ APIs through the Objective C bridging header, and then put some calls to Swift code in the oF Cocoa window code, and that’s it.

Thank you. Do you think it could be possible to have a tuto to create a wrapper for C++ / OF code. The use case of OF into swift is mostly using the playground function that makes possible to see in real time the result of your code.

While within the realm of possibility, I would think it is unlikely to occur without a great deal of effort, especially if anything resembling idiomatic Swift were desired.

Bindings aren’t something I have a great deal of experience with and I believe it is a deceptively deep topic, but I’d look at the Lua / Python / AngelScript oF bindings others have created and discussed, and take notice their challenges, even with proper tooling support like SWIG. There was a good thread on the of-dev list python bindings + static (or dynamic) lib (not included in the December archives which seemed to ) that discussed several complexities and strategies to binding generation. Even for languages that have a history of C++ binding and well supported toolchains (typically SWIG), it is non-trivial - so add a young and rapidly evolving language like Swift to the mix and…

If you wanted to try your hand with Swift bindings, two projects that might be worth a look are swiftpp and djinni.

I suspect there is a lot more involved in bringing up oF within a Playground than simply Swift bindings as now you want to integrate oF into something else. If your end goal is a visual REPL-like environment for oF, there are a couple options to do that today, the two I can think of off the top of my head are:

  • @bakercp and co’s ofSketch is an IDE of sorts and pairs an oF app to do the heavy lifting with user input and previewing in the browser
  • @danomatika’s Visual is an environment that can run Lua scripts that use oF bindings

A quick clarification on earlier statements: the shim layer would need to be C or Objective-C++ as Objective-C itself doesn’t have any C++ interop. :wink:

1 Like