Exploring constants and variables

Now that you know about the different simple data types that Swift supports, let's look at how to store them, so you can do operations on them later.

You can use constants or variables. Both are containers that have a name and can store values, but a constant's value can only be set once and can't be changed once it's set.

You must declare constants and variables before you use them. Constants are declared with the let keyword and variables with the var keyword.

You may have noticed that the names for constants and variables start with a lowercase letter, and if there is more than one word in the name, every subsequent word starts with a capital letter. This is known as camel case. You don't have to do this, but this is encouraged, as most experienced Swift programmers adhere to this convention.

Now, let's explore how constants and variables work by implementing them in your playground:

  1. Add the following code to your playground and click the Play/Stop button to run it:
// Examples of constants
let theAnswerToTheUltimateQuestion = 42
let pi = 3.14159
let myName = "Ahmad Sahar"

These are examples of constants. In each case, a container is created and named, and the assigned value stored.

  1. Enter the following code and run it:
// Examples of variables
var currentTemperatureInCelsius = 27
var myAge = 50
var myLocation = "home"

These are examples of variables. Similar to constants, a container is created and named in each case, and the assigned value stored.

You should see the stored values displayed in the Results area.

  1. Add the following code and run it:
// Value of a constant can't be changed once it is set
let isRaining = true
isRaining = false

As you're typing the second line of code, note that a pop-up menu will appear with suggestions:

Use the up and down arrow keys to choose the isRaining constant, and press the Tab key to select it. This feature is called autocomplete and helps to prevent typing mistakes when you're entering code.

When you have finished typing, wait a few seconds. On the second line, you should see a red circle with a white dot in the middle appear:

This means there is an error in your program, and Xcode thinks it can be fixed. The error appears because you are trying to assign a new value to a constant after its initial value has been set. Click the red circle.

  1. After you have clicked the red circle, you should see the following box with a Fix button:

Xcode tells you what the problem is (Cannot assign to value: 'isRaining' is a 'let' constant) and suggests a correction (Change 'let' to 'var' to make it mutable). Click the Fix button.

  1. After you have clicked the Fix button, you should see the following:

Hey presto! No more errors! Do note, however, that the suggested correction might not be the best solution. 

If you look at the code you typed in, you might be wondering how Xcode knows what type of data is stored in a variable or constant. You'll look at that in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.153.38