Instruction
Writing code might seem frightening in the beginning, but it really isn’t. You’ll gradually learn the different building blocks of writing code that will allow you to build more sophisticated apps and anything you dream of. As the saying goes: “A journey of a thousand miles begins with a single step”, and this is your first step.
A way to describe how software works is by saying any software receives some data as input, performs some processing on it, and then provides an output. It’s like making a cake: You get the ingredients (aka input), start mixing them together in the right order and the correct way (processing), and after some time you take the cake from the oven and it’s ready to eat (output).
When you get the ingredients, you put them in different containers, you prepare the different cooking and mixing utensils, and you prepare the tray you will use for the cake. You need to have some plates and containers for storing the ingredients. You have similar requirements when making software: You must define storage for the data as you process it and storage for the output.
Declaring Variables
Declaring a variable or storage unit in Swift consists of four parts:
- Use the keyword
varto define if this storage allows the value to change later in your program, or useletto show the value is constant and isn’t allowed to change. - The name you want to give for this stored value. In this example, the name is
exampleVariable. - The type of data you’ll store. It’s written as a colon (:) followed by the type name. The type for a number isn’t the same as text, for example. You must describe in your application what you want to store so Swift can help you use it correctly. In this example, the type is
Int, which represents integer numbers. - The initial value you want to store. If
vardefines your stored value, you can skip writing this part, because the value can be changed later. But ifletdefines it, you usually include it because you can set the value of a constant only once. In this example, the initial stored value is10.
You can create as many variables as you need in your program. You just need to ensure variables that live in the same part of your program or scope have different names; otherwise, Swift won’t know which one you need to access. Don’t worry, though: If you accidentally make this mistake, the computer will tell you. :]
Primitive Datatypes
There are plenty of different types you can use in Swift, but to get started, let’s focus on the primitive types:
-
Bool: Boolean type, which can be eithertrueorfalse. -
Int: Integer type, which can store whole numbers without fractions. -
Float: Floating-point type, which can store fractions. -
Double: Double precision floating-point, which can store fractions with much more precision thanFloat. -
String: As its name suggests, it can store text.
Float and Double are very similar. To highlight the precision difference between them, Float can store the mathematical pi number to the 6th decimal point —3.141593 — while Double can represent it to the 15th decimal point — 3.141592653589793. The word double comes from it taking twice the amount of memory Float uses to store a number.
In the next demo, you’ll use Xcode’s Playground to create a basic calculator and use the four data types: Bool, Int, Float and Double.