loading...

The Swift Programming Language Companion: The Basics

loufranco profile image Lou Franco Updated on ・2 min read

This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple.

The Swift Programming Language Companion

The Basics

So, to recap, we have the book and Xcode side-by-side. We're going to read the first chapter, The Basics, and write code in the Playground.

Before you start, if you have an app idea, keep that in mind. If you don't, just pick some app you use and know well and keep that in mind. As an example, I am currently working on a workout watch app that I will use to manage my running. I will show you how I use that to generate exercises for myself.

Constants and Variables

When you read this section, you are going to learn about how to declare constants and variables. The book gives several examples. Type them into the Playground if you wish.

Then, generate similar code based on your app idea. For example, for my app:

let numberOfLaps = 5
var currentLap = 0

var workoutName: String 
workoutName = "5 x 100m"

The other very important thing that was introduced is that there is a way to describe the code with a sentence. The first line above can be read as: "Declare a constant called numberOfLaps and set its initial value to five".

So, as an exercise,

Declare a variable called numberOfMinutesRunning and set its initial value to 0

Then,

Change the value of numberOfMinutesRunning to 1

What's next

This chapter covers declarations and types, but starts getting more complicated at Optionals. So, read through, type in code, generate more code based on some app you keep in mind and in the next post, I'll dig a little deeper on Optionals.

If you have any trouble, ask in the comments below.

Posted on by:

loufranco profile

Lou Franco

@loufranco

Software Engineer on Trello iOS for Atlassian.

Discussion

markdown guide