Scripting in Swift: short introduction- 4 mins
Surströmming, salmiakki and automation.
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.
So, what do you need to start scripting in Swift? The list is fortunately not so long:
- macOS. I only used Swift on Apple’s operating system. There is a possibility to run Swift on Linux, but, I’ve never played with that so I couldn’t tell anything more.
- Command Line Developer Tools. If not installed, it will be automatically downloaded when needed. Don’t bother about this.
- Text editor. Just a matter of preference - you need a tool to write your scripts. I prefer Sublime, but it is really up to you.
- Xcode (optional). This tool is really powerful, and for simple scripts, it can be too heavyweight. However, code completion, `jump to definition` option and access to Foundation’s documentation could be useful.
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`.
Now you can type Swift expressions - REPL will execute them and print output.
You can declare variables, constants, and even use functions.
If you need to leave REPL just write `:q` and press enter.
To learn more about Swift REPL, I recommend official blog post.
Now we will leave REPL and move to writing very simple Swift script. First, we need to create Swift file:
Now, we can open HelloWorld.swift file and add some Swift code.
To execute our first script, just type `swift HelloWorld` in Terminal.
We can also make files executeble. Before setting as executable, we need to remember about putting shebang in our HelloWorld.swift file.
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:
I think the code is self-explanatory, but there are three really important aspects:
- taking command line arguments
- URLSession usage
- doing something usefull
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.