Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

array
An ordered list of values of one type, reached by position starting at zero.
block
A group of lines of code that run together, often inside curly brackets.
closure
A piece of code you can store in a value and pass to other code, remembered with the names around it.
compiler
The program that turns the code you write into something the machine can run, reporting mistakes before it does.
dictionary
A collection of key and value pairs, where each key appears once and finds its value.
enum
A type whose value is one of a fixed set of named cases, which may each carry extra data.
expression
A piece of code that produces a value, such as a sum or a call to a function.
function
A named piece of code that takes values and returns a result when called.
guard
A check that must pass before the code after it runs, leaving the scope early when it fails.
method
A function that belongs to a type and is called on a value of that type.
nil
The absence of a value, allowed only where a type says a value may be missing.
operator
A symbol such as plus or equals that combines values into a new one.
optional
A type that either holds a value or holds nothing, written with a question mark after the type.
optional chaining
Writing a question mark before something you reach into, so the result is nil when the value is missing.
property
A named value that belongs to a type or to one of its values.
protocol
A list of requirements that a type promises to meet, so code can work with any type that meets them.
scope
The region of code where a name can be used before it goes out of reach.
string interpolation
Putting a value inside text with a backslash and parentheses, so the value becomes part of the text.
struct
A type that groups related values and makes a copy whenever it is given another name or passed along.
subscript
A way to read or write an item in an array or dictionary by putting a key or position in square brackets.
type
What kind of value something is, such as a whole number or text, and what it can do.
unwrap
To get the value out of an optional after checking that something is there.

Swift from the ground up

Your first Swift program

What Swift is for, how a program is a list of steps, and how to print a line of text on the screen.

Part 1 of 12

Beginner12 min readBack to Courses

Swift, and where you meet it

Swift is a language made by Apple for phones, tablets and computers, and it also runs on servers. It reads a lot like English, and it refuses many small mistakes before the program ever starts, which is why it feels friendly at the beginning.

You write a file of plain text. This page sends that text to a Swift compiler and shows you what the program printed, so you can change a line and see the new answer straight away.

The compiler here is the open-source toolchain, not Xcode. The same compiler that builds a phone app can build a command-line program, and this page uses that smaller shape: one file, no window, just text in and text out.

A program is a list of steps

At the top level of the file you simply write the steps in order. Swift runs the first line, then the next, then the next, until it reaches the end and stops.

Two slashes start a comment. The compiler skips the rest of that line, so a comment is a note for the reader and nothing more.

A worked example makes the order concrete. Write a let for a price, print it, then write a second let that doubles it. The first print shows the original number; move it below the second let and it shows the doubled one. Only the position changed, which is why reading top to bottom predicts the output.

Printing a line

print(...) takes what you give it and writes it out with a line break after it. You can hand it several things separated by commas, and Swift joins them with a single space.

Text goes inside double quotes. A name such as name is not text itself. It stands for whatever value you gave it earlier, and print puts that value on the screen.

A comma is the simplest way to build a line. print("Total:", 12) shows Total: 12, and a third piece gets another single space. The pieces stay separate values; print turns each into text and adds the spacing, so two words never silently merge.

What the compiler checks first

Swift reads the whole file before it runs the first line. Every name has to exist in the file before the program can use it, so a misspelled name is reported before anything prints, not halfway through the output.

Line breaks separate one statement from the next, and indentation is for the reader only. The compiler follows the braces and the quotes, which is why one missing quote can make several correct lines look wrong.

That is why the compiler points at the line where the quote opened, even when the error only becomes visible several lines later.

The compiler works in stages. It first checks the shape of each statement, catching a missing bracket or an unterminated string. Then it resolves names and types, where an unknown variable or a reassigned let is reported. Only after both pass does it run anything.

What goes wrong, and how to read the message

The most common first error is an unterminated string. print("Hello) leaves the closing quote off, so the compiler keeps reading and treats the following lines as text. It reports the problem at the end of the file, where the string finally runs out, so look upward for the quote with no partner.

The second is a name that does not exist yet. At the top level Swift reads in order, so a print that mentions total before the line that creates it fails. Move the definition above its use, but check the spelling first, because a typo gives the same message.

The third is a let given a second value. The compiler points at the assignment and explains that the value cannot change. That is a signal that the name was meant to vary, so change let to var; or notice that the second assignment was a mistake and delete it. Choosing between those repairs is what the next chapter is about.

The smallest Swift program

// Two slashes start a comment.
print("Hello, Swift!")

let who = "reader"
print("Hello,", who)

print("One", "line", "of", "words")

Three mistakes catch almost every first program. A missing closing quote turns the rest of the file into text, and the compiler reports the problem at the end rather than at the quote. Printing a name inside quotes — print("Hello, name") — prints the word, not the value, so leave the name outside and put a comma before it. And a let refuses a second assignment, so use var for a value you intend to change.

Run it, then make it yours

Press Run and read the four lines of output. Then change the name, and add a line of your own that prints a greeting and a number.

Output
Click "Run" to execute your code

Try these next

  • Print three lines about a place you know, one fact per line.
  • Put a name in a let, then print it once after a comma and once beside a second value on the same line.
  • Change a let to a var, give it a new value, and print the value before and after the change.

An order, printed in pieces

Run it and match each printed line to the value it came from. Then change the quantity and add a second item, and predict the new total before you run it again.

Output
Click "Run" to execute your code

Swift is strict about capital letters. A name you invent is case sensitive, so count and Count are two different things, and the compiler will tell you when a name is unknown.

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What does the print function do?