Getting Started
In this chapter, you’ll get started with Swift and write your first Swift program.
Setting up a development environment
Before you can write Swift code, you need to set up a development environment. At a minimum, this consists of the Swift toolchain and an editor or IDE that supports Swift.
Our Swift Setup repository contains installation instructions for the various platforms that support Swift and for our recommended editor, which is Visual Studio Code. Follow the instructions in the repository and verify that you have Swift 6.2 or later installed, as that’s the minimum version you’ll need for this course.
Using a command line interface
Throughout this chapter, you’ll use a terminal to compile and run your code. A terminal is an application that provides a command line interface (CLI) through which you can control your computer by typing commands.
Using a terminal can be daunting at first, but once you know the basic commands, it’s not too hard. Command line skills are essential for any programmer, which is why we don’t shy away from using a terminal in this course. Of course, you don’t need any prior experience with using a command line to complete this course. The instructions in this chapter will show you the commands you need.
On macOS and Linux, you use the Terminal application to access the command line. Windows has two different command lines: Command Prompt and PowerShell. Both are available as stand-alone applications, or through the new Windows Terminal.
Your first program
Open your editor, create a new file, and enter the following:
print("Hello, world!")
Save this file as hello.swift in a directory named Projects in your home directory. Next, you’ll use a terminal to navigate to this source file, compile it, and execute the resulting binary.
Open your terminal. The terminal should start in your home directory. Verify this:
pwd
If you’re not in your home directory, navigate there as follows:
cd
Now navigate to the Projects directory:
cd Projects
List the contents of this directory:
ls
Verify that hello.swift is in this directory before continuing.
Next, invoke the Swift compiler (swiftc) to compile your source file and produce an executable binary:
swiftc hello.swift -o hello
This command produces an executable binary named hello (on macOS and Linux) or hello.exe (on Windows). You run this binary as follows:
./hello
As you can expect, this prints the text Hello, world! in the terminal.
Note
The leading ./
(on macOS and Linux) or .\
(on Windows) is required here because the executable is in the current directory — represented by a single dot — and not in the usual directories where the command line searches for its commands.
Compiling and executing multiple files
Most programs consist of multiple source files that are combined into a single executable. To illustrate how this works, use your editor to create two new files: square.swift and main.swift.
In square.swift, put the following code:
func square(_ n: Int) -> Int {
n * n
}
In main.swift, put this:
for n in 1...20 {
print(square(n))
}
Together, this code prints the squares of the numbers 1 through 20.
To compile both files into a single executable, you specify them both as arguments to swiftc:
swiftc main.swift square.swift -o squares
You run this executable as before:
./squares
When your program starts, it executes the code in main.swift from top to bottom. This code can use the code from square.swift because it’s part of the same executable.
Using Swift packages
Programs can quickly grow to tens or even hundreds of source files. When that happens, you want to rely on tools to manage your files and invoke the compiler for you.
Swift includes such a tool: the Swift Package Manager (SwiftPM). This section describes how you can use the Swift Package Manager to create a Swift package, compile it, and run your program.
To get started, create an empty directory named SquaresApp in the Projects directory, and navigate into it:
mkdir SquaresApp
cd SquaresApp
Invoke the swift package init command to create an executable package:
swift package init --type executable
This command generates the following files and directories:
Package.swift
A package consists of one or more components known as targets, each with its own source files. Package.swift configures your targets and how they depend on each other. A package can even depend on other packages.
In this case, Package.swift configures a single executable target named SquaresApp that has no dependencies.
Sources
This directory contains the source files for your package, arranged into subdirectories for each target.
Every executable target must have a main.swift or equivalent file, in this case SquaresApp.swift. This is the starting point for your program. A target that doesn’t have a starting point is a library. You can’t execute a library; you can only include it as a dependency in other targets.
.gitignore
This hidden file is part of the Git version control system. Git is a tool that manages your files, tracks the changes you make, and allows multiple people to work on the same source files.
Git is an essential tool for software developers; however, it’s beyond the scope of this course, so you can ignore this file for now.
Now that you understand the structure of your Swift package, you can start adding your files to it.
Move the main.swift and square.swift files you created earlier from the Projects directory — represented by the two dots — into the Sources ▸ SquaresApp directory:
mv ../main.swift ../square.swift Sources/SquaresApp
Delete SquaresApp.swift as your program can only have one starting point:
rm Sources/SquaresApp/SquaresApp.swift
Now compile your package with the swift build command:
swift build
This command compiles your code and generates an executable in the hidden .build directory. You can run this executable as follows:
.build/debug/SquaresApp
Alternatively, you can use the swift run command to compile and execute your code with a single command:
swift run
Using the Swift Package Manager is far easier than invoking the compiler directly. The latter is not something you should have to do as a novice programmer, so now that you understand how compilation works, stick to using packages for the remainder of this course.
Visual Studio Code even supports creating and running packages straight from the editor, without the need for typing commands. Our Swift Setup repository shows how to do this.
Avoiding concurrency errors
Applications are often able to perform multiple tasks concurrently. A browser, for example, can download files in the background while you continue browsing. Adding concurrency can increase the performance of an application. However, mistakes are easy to make and may cause unpredictable behavior and hard-to-find bugs. Fortunately, Swift can detect these mistakes and report them as errors.
Swift aims to be safe by default, so it checks for concurrency issues in all applications, even those that don’t require it. None of the applications you’ll build in this course involve concurrency, so any errors reported will be false positives. To avoid these errors, add the following setting to your target in Package.swift:
.executableTarget(
name: "SquaresApp",
swiftSettings: [
.defaultIsolation(MainActor.self)
]
),
Do this for every package you create throughout this course. You’ll learn more about concurrency in a future course, at which point you’ll no longer need this setting.
Up next
Now that you know how to create and run a Swift program, you’re ready to start your programming journey. In the next chapter, you’ll learn how you can use operators and functions to perform basic calculations.