Using (a restricted) Swift on Any Operating System

Ohhhh! Mighty Apple! Thy beautiful syntaxes yee have bestowed upon thee are trapped behind a wall of sharp wire. How must we work or play or be hired?

Swift is an awesome general programming language, but it stops there. If you're reading this then you know what Swift is and what I'm talking about. If not look it up.

Apple does not support anything other than their platform, Mac OS X. Swift is also proprietary. Closed source. Secret. This really, really sucks. It may as well be non-existent in my (and the thousands of others') (FOSS) world. The only reason why I'm using Swift is because I've been exposed to it in my mobile application development course - but I'm glad I have had a taste, because it tastes bitter sweet.

Unfortunately you can't do anything with Swift on Debian.

Unless you whip out your hammer and keyboard and get to work. 

You'll need the hammer to repent for your sins later.

So how do we get around this? Well, one solution is to setup a web service that takes code, pipes it into the Swift compiler on a MAC OS X virtual machine, and output it back to users. This is the solution we're going to use to enable us to do Swift development on any operating system (although still very restrictive).

[user types in code, presses send] <-> [web server on mac] <-> [swift compiler]

We will shortly learn though after the fact that this "solution" is actually so restricted you might as well stop before you even start coding any Swift.

First we have to pick a service. I've chosen as my "online compiler". It will return any errors and the output of the program. Once you've done that, you need to inspect what the client-side does to send your code to the service.

We can see in the picture below that the client does a POST to /api/v1/swift. Cool. But what does it send in the POST?

(me after double clicking the "run" row)

Oh, perfect! Nice and simple. The client sends simple form data with two fields: api_key and task[code]. So if we make a program that sends a POST request to with a media type application/x-www-form-urlencoded and the two fields api_key and task[code], we can compile Swift!

But wait, what should be we expecting to get back?...Well lets check it out!

We are greeted with a nice simple JSON with 3 attributes: code, output, and success.

So after figuring all that out - the next step makes everything work nicely if your editor plays nice with UNIX piping.

I've written a Haskell program that simulates what the client side of my browser was doing on It takes my code, and the API key (subject to change per user? I haven't needed to change it.), wraps it up and sends it off in a POST. Then it waits for a response, parses the JSON and gives us the good stuff.

You will need to install http-conduit and aeson packages to compile this (you can try by running cabal install http-conduit aeson; ghc swift-compile.hs; on your computer).

After you've compiled it, install it to /usr/local/bin/swiftc so we can use it anywhere on the system.

Now using your new program with your editor. In this little guide we'll be using Emacs. Emacs has a nice command called M-| (or in regular user-speak Alt-Pipe). What this does is pipe your selected text to a program, and anything that program outputs will be put into a separate window (frame/buffer) beside it.

So if you fire up Emacs, you can follow along like this:

  1. File -> Open
  2. Open up your Swift file
  3. Select all the text you want to be compiled
  4. Hit Alt-Pipe (M-|) and then type swiftc
  5. Wait for a few seconds, and Boom! A new window (called a frame in Emacs) will open up with the output. If there are any errors it will print the error.

Here is an example of what you'd see. My script doesn't pretty print the JSON, yet...(cabal issues, ugh!):

Not a good example though, considering I was trying to compile Haskell as Swift.

Using this setup though I wasn't able to do much and would have to wait until I had access to a Mac computer to do the majority of the complex work. The reason for this is because these online compilers restrict you to the Foundation library. You can't import anything else. And this is where Swift starts to really suck again.

If you are a person from Apple who happens to be reading this - convince whoever to open up Swift! It is such a refreshing language.

To the others - good luck. We're counting on you.


Popular Posts