Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Preface

This book teaches Swift to someone who already programs. If you have written loops and functions in Lux, Python, JavaScript, or anything of that family, you have what you need. You will not be told what a variable is. You will be told what Swift does with one, which is a different and more interesting question.

Three things will be new. The first is that types are checked before the program runs rather than while it runs, so a class of mistake that a scripting language reports at three in the morning is reported by the compiler at your desk. The second is that Swift has no null: a value that might be missing is wrapped in an Optional and the compiler will not let you forget to open it, which is Chapter 5. The third is the one that takes longest to feel natural. Swift asks you to decide whether a value is copied when you pass it around or shared, you make that decision every time you define a type, and it changes what your program means. Chapter 7 is where that gets its due.

The pace is brisk on purpose. Programs come first and explanation follows, and the explanation stops when the point has been made. A feature you can already guess the shape of gets a sentence. A feature that will bite you gets a page. If you want the complete description of some corner of the language, The Swift Programming Language is free online and this book is not trying to replace it. What this book is trying to do is get you writing real Swift in a week rather than a month.

Read it with a keyboard. Type the programs in, run them, then break them and read what the compiler says. The exercises are short and most of them are variations on the program just presented; do the ones that look interesting and skip the rest without guilt. Each has a worked answer in examples/solutions, which is a worse place to start than your own keyboard.

Getting the compiler

Install Swift from https://www.swift.org/install/. There are packages for macOS, Linux, and Windows, and swiftly will install and manage toolchains on macOS and Linux the way rustup does for Rust. You do not need Xcode and none of the programs here use an Apple framework. Check the install with

swift --version

Everything in this book is Swift 6, compiled in language mode 6, which is what you get by default from a Swift 6 toolchain in a package and what the -swift-version 6 flag asks for when compiling a single file. Strict concurrency checking is part of that mode and it stays quiet until Chapter 12, where it becomes the point.

About the examples

Every complete program in this book is a real file in the examples directory beside the text, and the text quotes the file rather than a tidied-up version of it. Compile one on its own:

swiftc -swift-version 6 examples/ch01/fahr2.swift -o fahr2
./fahr2

On Linux, a single file that calls a floating-point function such as rounded() may fail to link with undefined reference to symbol 'round'. Add -lm, which is harmless everywhere else, or import Foundation, which pulls it in as a side effect. The checker described below compiles with -lm for that reason.

Chapter 11 builds a program properly, with a package, modules and tests, and from there on you use swift build and swift run instead.

Every output printed in this book came from running the program, and every compiler message quoted came from making the mistake. tools/check.sh compiles each program, runs the ones that have recorded output, checks that output still matches, and confirms that the code printed in the text is identical to the code on disk. If something here does not behave the way the text claims, the text is wrong and you should trust the compiler.

The programs that deliberately fail to compile live in examples/errors, one per mistake, and Appendix C collects their messages.

A note if you are coming from Lux

Lux already taught you a good deal of Swift’s data model without naming it. Values are immutable unless you say otherwise, there is no null, a function that might fail hands back an Option or a Result, and match has to cover every case. All four of those are Swift, spelled let, Optional, throws, and switch, and they are the parts of Swift that people arriving from Python find strangest. You are further along than you think.

You also have a shortcut nobody else gets. lux convert swift translates a Lux program into Swift source, so a program you already understand can be read back to you in the new syntax. Do that with something you wrote and you will recognize most of this book before you start it. Appendix B maps the two languages construct by construct, along with Python and JavaScript.