Swift Fundamentals

Playgrounds

Swift Fundamentals
1 Playgrounds 04:22
2 Variables, Constants, and Types 05:38
3 Arrays and Sets 08:08
4 Dictionaries 03:28
5 Optionals 04:41
6 Working With Optionals 08:53
7 Tuples 06:29
8 Conditionals 07:39

Swift is an easy language to get started with. The syntax is easy to understand and reminiscent of other popular programming languages you may already be familiar with. In Swift Fundamentals, you learn the fundamentals of the Swift programming language.

Swift. A modern programming language that is safe, fast, and interactive.

Playgrounds

Apple introduced Swift in 2014 at WWDC. At the same time, the company added interactive playgrounds to Xcode. Playgrounds are without a doubt the best way to learn the Swift programming language. Let me show you why that is.

Creating an Interactive Playground in Xcode

Open Xcode and create a new playground by selecting New > Playground... from the File menu. Choose Blank from the list of iOS > Playground templates.

Creating an Interactive Playground in Xcode

Enter a name for the playground and tell Xcode where you'd like to save the playground on your machine. Click Create to create the playground.

Creating an Interactive Playground in Xcode

Exploring the Playground in Xcode

The playground is split into two sections, the code editor on the left and the results panel on the right. Xcode has populated the playground with three lines of code, a comment at the top, an import statement, and a variable declaration.

Exploring the Playground in Xcode

What makes playgrounds great is their interactive component. A playground contains a Swift REPL. REPL stands for Read-Eval-Print-Loop. The Swift REPL reads the contents of the playground, evaluates the code, and displays the output in the results panel on the right. This process is repeated for every change you make in the editor.

Let's try it out. Change the string that is assigned to the str variable from Hello, playground to Hello, world. The results panel automatically reflects the change you made.

Exploring the Playground in Xcode

Comments

The playground contains only three lines of code, but there are a number of things we can learn about the Swift programming language by taking a closer look at these lines of code. The first line of code is a comment. Single-line comments start with two forward slashes, //.

//: Playground - noun: a place where people can play

Swift also supports multi-line comments. They start with /* and end with */. Try it out by adding the following multi-line comment to your playground.

/*
 This is a multi-line comment.
 It doesn't appear in the results panel.
 */

As in other programming languages, comments are ignored by the compiler and Swift is no different. The comments are not visible in the results panel on the right.

Modules

The second line of code is an import statement for the UIKit framework. The import keyword is followed by the name of the module, UIKit in this example.

import UIKit

This brings us to the question "What is a module?" The Swift language defines a module as a single unit of code distribution. This can be a library or a framework, but an application is also considered a module.

By importing the UIKit framework, we gain access to the classes, protocols, and constants defined in the framework. Xcode added the import statement for the UIKit framework because we chose the blank iOS template from the list of templates when we created the playground. Every iOS application relies heavily on the UIKit framework and that is why Xcode added the import statement for us.

Variables and Constants

The third line of code is a variable declaration. The variable is named str and is of type String. Values can be stored as variables or as constants. A variable is declared using the var keyword and a constant is declared using the let keyword.

You can change the variable str to a constant by replacing the var keyword with the let keyword. The output in the results panel remains unchanged.

let str = "Hello, world"

In the next episode of Swift Fundamentals, we take a closer look at variables and constants.

Semicolons

You may have noticed that none of the statements in the playground end with a semicolon. While it's fine to end a statement with a semicolon, the best practice is to omit them. The compiler is clever enough.

What's Next?

In less than five minutes, you already learned a lot about the Swift language. In the next episode, we cover variables and constants in more detail.

Next Episode "Variables, Constants, and Types"

Stop Writing Swift That Sucks

Download the Swift Patterns I Swear By