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 and editors that support Swift. For this course, we recommend that you use one of the following combinations:
- macOS with Xcode or Visual Studio Code
- Ubuntu or Fedora Linux with Visual Studio Code
- Windows with Visual Studio Code
Follow the instructions in the repository and verify that you have Swift 5.9 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 ./
or .\
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 the SquaresApp target. In this case, it contains a main.swift file that prints the text
Hello, world!
.Every executable target must have a main.swift or equivalent file. 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 in the previous section from the Projects directory into the Sources directory, overwriting the existing main.swift:
mv ../main.swift ../square.swift Sources
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
Once youβve created a Swift package, you can use your favorite editor or IDE to edit the source files. Our Swift Setup repository explains how to do this.
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.