Scripting in Swift: short introduction

- 4 mins

Surströmming, salmiakki and automation.

Intro

Two months ago, I’ve heard that there is a possibility to write and execute scripts in Swift. Damn, why it is so niching? Why I wasn’t aware and I struggled with everything in bash?

Compiled Languages vs Scripting Languages

Before we start, let’s recall the basics. There is recognized dichotomy of programming languages. Compiled languages require compiler, which translates our source code into binary. Then binary is run on the machine. This is what we used to do when it comes to developing iOS and macOS apps.

Scripting languages give a possibility to run a source code directly in some other application (called interpreter). As there is no compiling step, we don’t need compiler. In this case, development is usually faster and simpler, but performance is much lower. Scripting languages are very often use for automation and avoiding repetitive tasks.

That’s needed to say: compilation versus scripting is not determined by language per se. It is a matter of adopted tools and solutions. There are languages that can be both scripting and compiled, and Swift is one of them.

Prerequisites

So, what do you need to start scripting in Swift? The list is fortunately not so long:

REPL

REPL, which is an acronym of read-eval-print-loop is a simple, interactive development environment. It gives a possibility to write single code expression, execute it, and immediately print the result. Let’s take a look.

Open Terminal app and type `swift`.

Welcome to Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2). Type :help for assistance.
1>

Now you can type Swift expressions - REPL will execute them and print output.

1> print("Hello World")
Hello World
2>

You can declare variables, constants, and even use functions.

1> let x = 2
x: Int = 2
2> func duplicate(number: Int) -> (Int) {
3.     return number*2
4. }
5> duplicate(number: x)
$R0: Int = 4
6>

If you need to leave REPL just write `:q` and press enter.

To learn more about Swift REPL, I recommend official blog post.

HelloWorld.swift

Now we will leave REPL and move to writing very simple Swift script. First, we need to create Swift file:

$ touch HelloWorld.swift

Now, we can open HelloWorld.swift file and add some Swift code.

print("Hello World")

To execute our first script, just type `swift HelloWorld` in Terminal.

$ swift HelloWorld.swift
Hello World

We can also make files executeble. Before setting as executable, we need to remember about putting shebang in our HelloWorld.swift file.

#!/usr/bin/swift
print("Hello World")

This small line of code in top of the file is responsible for running script using proper interpreter. We can make file executeable using chmod +x or swiftc:

$ swiftc HelloWorld.swift
$ ./HelloWorld
Hello World

Selenops

Writing `hello world` using new tools is always exciting as hell, but let’s take into account something more serious. Selenops is a very simple web crawler, developed by Federico Zanetello.

I think the code is self-explanatory, but there are three really important aspects:

With scripting in Swift we can enjoy all the benefits of powerful Foundation (and even external dependencies), but what it is more important, we can develop stuff that can solve real problems.

To be continued…

Scripting in Swift is great. iOS developers are usually not very fluent in anything else than Swift or Objective-C. I can write some shell scripts, but on my scale of pleasure, it is somewhere between eating Swedish surströmming and Finnish salmiakki. Swift gives us the power of automation using our well-known tools in our well-known world. We are no longer doomed to use Shell scripts, Python or Ruby. This is so fascinating, that you can expect next posts on this topic very soon.

Aleksander Popko

Aleksander Popko

iOS & macOS Software Engineer

comments powered by Disqus
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora