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.
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.
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
letto avar, 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.
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?
