Hello Swift!

  Tutorials     August 19, 2015  

OK, so… it’s time we get started, isn’t it?

Now – as you will soon realize – when it comes to coding, I pretty much hate talking (though I simply love it under a different context lol) and prefer to let the code speak for itself – after all we’re coders, right?

Before getting are hands dirty and discovering what Swift is all about,ย it’s time for an all-time classic Hello World.

Our first program

So, fire up your favorite editor (if you still haven’t made up your mind – self-promotion coming lol – have a look at Peppermint) and let’s write this very… complicated one line of code.

print("Hello Swift!");

Now we already have our code file, let’s save this as hello.swift.

The next rather logical question is: how do we actually run this thing?

Executing it

Well, you might be expecting something along the lines of “Open Xcode, etc, etc” – but since I’m rather old-school and prefer having total control over what goes on, why/how it is executed, what could be better than doing it straight from the command line?

So, just open Terminal and let’s go to the location where your hello.swift resides. Now, all there’s left – provided that you already have Xcode installed – is running our script:

xcrun swift hello.swift

And this is what it looks like:

Screen Shot 2015-08-19 at 11.46.42 AM

That was easy, wasn’t it? Pretty much like with any scripting language (see: php, python, ruby, perl, etc). But wait: is Swift a scripting language? Well, not quite…

Compiling it

What if we actually want to compile this code of ours and have a binary, which we can then use — without having to re-compile or having the Swift compiler around?

Let’s see…

xcrun swiftc hello.swift

Super-easy again. Just note the use of swiftc (the swift “compiler”) instead of swift. Now, if you look again into your folder contents, you’ll notice a single hello binary, which you can execute – as any normal binary


And that was it.

Have fun – and welcome to the wonderful world of Swift! ๐Ÿ™‚