Lesson 1 – Writing and running your first Swift program in an Xcode playground

Xcode is the required tool for Swift programming on Mac computers

Swift is becoming more widespread as a programming language and is available on multiple platforms.

The primary use of Swift is developing apps for Apple platforms: iOS, iPadOS, macOS, watchOS, and tvOS. That means that app development in Swift happens mainly on a Mac computer using Xcode.

Xcode is an integrated development environment (IDE). In short, it’s an app with comprehensive functionality that helps developers write code. You get Xcode for free in the Mac App Store.

Download Xcode from the Mac App Store

Xcode is quite a heavy app, so make sure there is enough space on the disk. At the time of writing, Xcode has 12.6 GB in the App Store, but it actually needs much more space than that.

The App Store download is compressed. When uncompressed, Xcode takes 30.6 GB on the disk. But even that amount of space is not enough.

There should be enough space to both download and uncompress Xcode. That means at least 43.2 GB of free space should be available. Don’t be surprised if the App Store app will complain that there isn’t enough space on the disk since Xcode keeps getting more bloated with every release.

On top of that, downloading Xcode from the App Store can be slow, so be patient. But hey, take it as an exercise. It takes a lot of patience to be a software developer, especially when things don’t work as expected.

Create Xcode playgrounds to write and run Swift programs

This course doesn’t need all the power of Xcode and the developer tools that come with it. Those are required when making apps, but Xcode playgrounds are enough for now.

A playground is a file where Swift programs are written. This can be done in any text editor, but Xcode playgrounds also allow programs to be run and their results to be seen.

Create a new playground by selecting File -> New -> Playground… from the Xcode menu.

Creating a new playground in Xcode

Choose the Blank template from the iOS tab (it makes no difference, for now, which template is selected).

Selecting the blank template for an iOS playground in Xcode

Pick a name for the playground and choose where to save it.

Naming and saving an Xcode playground

The playground finally appears with some template code already inside.

The Xcode playground’s window

Here, you can write your first Swift program.

A computer program is made of a sequence of instructions that tell the computer how to perform a specific task. These instructions are made of simple text that will be converted into machine instructions that run on the computer’s CPU.

Time to write something. Delete everything in the playground and type the following code:

A program can be of any length. Our program is made of only one instruction which tells the computer to print “Hello World!” to the console.

Run Swift programs to see their results

Time to run the program to see its results. Press the play button in the bottom left corner of the playground’s window.

The button to run a Swift program in an Xcode playground

Another option is to press the play button, which appears in the gutter. This is the space on the left of the code where the line numbers are.

The play button in the gutter next to the code of a playground

The second method allows the program to be run until it reaches a specific line. Quite useless for a program with only one instruction, but it will be helpful later.

When running the program, another program called compiler reads the code and transforms it into binary code that can be understood by a computer (Swift is a language for humans, not machines). After that transformation, the code is executed on the computer’s CPU, producing some results.

Check out the result of the program in two places: the sidebar on the right side of the playground’s window and the debug area. The latter is brought up by pressing the button in the right bottom corner of the playground’s window on the same line as the play button.

Running a Swift program in a playground to see its result in the sidebar and the debug area

The sidebar will show even more helpful information for more complex programs. It also has buttons that offer more details about the result or make it appear immediately below the instruction that produced it.

The buttons to expand a result in an Xcode playground

Fix errors in Swift programs

The instructions in programs must follow the Swift syntax that you will learn in this course. You can’t type anything in a program.

The compiler will complain if mistakes are made, and Xcode will show error messages. Xcode detects errors as you type. If a program has mistakes, the compiler will stop at the first one and display a message in the debug area when the program runs.

Remove a letter from the print instruction.

The compiler flags an error for the above code.

An error in a Swift program inside an Xcode playground

Add the missing letter to the print instruction, and the error disappears.

The above error is trivial, so it is easy to fix. There will be even more errors as programs become more complex. This is normal, and dealing with them is one of the most difficult challenges while learning programming.

Use comments to annotate Swift programs 

Sometimes it’s important to add notes to code. As you learn Swift, it will be helpful to write down next to some code what it does. When making apps, it’s also beneficial to document:

  • what a specific piece of code does,
  • why some code has been written in that particular way,
  • what alternatives have been tried already and should not be tried again,
  • a workaround for some bug, etc.

Use comments to annotate code. A comment is a piece of text in a program that the compiler ignores. There are two ways to write comments in Swift:

  • Any text placed after the //characters is a comment until the end of the line; 
  • Delimit comments starting with the /* characters and ending with */. Any text between these delimiters is part of the comment, which means the text spans multiple lines.

During this course, I will use comments to show the result of some code without taking a screenshot of a playground.

Comments are also helpful to disable some lines of code the compiler should ignore temporarily. Xcode has a keyboard shortcut for that. Press cmd+/ on any line of code, and Xcode will toggle the // comment delimiter for the current line. It also works if more than one line in the editor is selected.

Register to get more exclusive free content

You can follow the entire course for free, with no registration required. Creating a free account gives you these extra benefits:

  • Enroll in the course and track your progress;
  • Download a free Swift cheat sheet;
  • Get access to other exclusive guides and courses available only to registered users.

Click here to register

Already have an account? Login here

 

 

 

Back to: Learn Swift > Language basics