Start with Swift

Introduction to the Swift programming language

An introduction to Swift, the statically typed, compiled language Apple created in 2014 to build apps for iOS, iPadOS, watchOS, macOS, and beyond.

This lesson is the beginning of the Swift series

Swift is a general-purpose programming language created by Apple and released in 2014.

It’s the main language for building new apps on Apple platforms: iOS, iPadOS, watchOS, macOS. It also runs on Linux and Windows, where people use it for servers, command-line tools, and cross-platform packages.

Swift is open source, compiled, and statically typed.

This is a complete Swift program:

let name = "Flavio"
print("Hello \(name)")

Run it and you get Hello Flavio. The \(name) part is string interpolation: it puts the value of name inside the string.

The Swift language guide is the official reference.

Swift is statically typed

Every value in Swift has a type, and the compiler checks those types before it builds the program. If something doesn’t match, you find out right away.

You can write the type explicitly:

let score: Int = 10

Most of the time you don’t need to. Swift infers it from the value:

let score = 10

In both examples score is an Int.

Swift is also type-safe. You can’t pass a String to a function that wants an Int. The compiler stops you.

Optionals make the absence of a value explicit:

var nickname: String? = nil

The ? says that nickname holds either a string or no value at all. Swift then forces you to handle both cases. We’ll spend a whole lesson on this.

See The Basics for the official explanation of types, constants, variables, and optionals.

Where can you use Swift?

Swift is closely tied to Apple platforms, but it’s not limited to them.

The official Swift platform support page lists macOS, Linux distributions, and Windows as development platforms. A Swift toolchain is the bundle you install: the compiler, the standard library, Swift Package Manager, and the development tools for your platform.

On a Mac, Xcode includes everything you need to build apps for Apple platforms: a visual interface builder, a simulator, a debugger, and code completion.

Swift Package Manager handles dependencies and builds packages. It comes with the toolchain.

How to start learning Swift

If you have a Mac or an iPad, Swift Playground is a friendly place to begin. You type a few lines and see the result right away, without creating a full app.

On a Mac, install Xcode when you’re ready to build a real app.

On Linux or Windows, follow the official Swift getting started guide to install the toolchain.

Then create a small executable package from the terminal:

mkdir HelloSwift
cd HelloSwift
swift package init --type executable
swift run

The last command compiles the package and runs it. You should see Hello, world! in the terminal. Open the main.swift file inside Sources, change the text, and run swift run again.

The goal of this series is to get you comfortable with Swift from zero. Continue with:

Lesson completed