Variables and Constants - Swift Academy: The Stress Free Way To Learning Swift Inside & Out (2014)

Swift Academy: The Stress Free Way To Learning Swift Inside & Out (2014)

Chapter 2. Variables and Constants

Variables and constants link a name (e.g. welcomeMessage) to a value from a certain data type (e.g. “Hi”). You can always change the values inside a variable whenever you need to. However, you can’t modify the value stored inside a constant once it has been declared.

How to Declare Constants and Variables?

You have to declare constants and variables before using them. You can declare variables using “var” and constants using “let.” Both “var” and “let” are Swift keywords. The following screenshot will help you understand how constants and variables work:

You can read that code as:

“Declare a constant and name it “maximumNumberOfLoginAttempts”. Then, assign 10 as its initial value. Next, create a variable named “currentLoginAttempt,” and set 0 as its initial value.”

In the example given above, you will set the highest number of possible login attempts as a constant, since the highest value will never change. The counter for login attempts is created as a variable, since its value will increase by one each time a user fails to login.

Swift allows you to declare multiple variables or multiple constants using a single line. You just have to place a comma between each entry. Here’s an example:

x = 1, y = 2, z = 3

Type Annotations

You may add a type annotation into your variable or constant declarations. This kind of annotation helps you specify the values that the variable or constant can hold. To write type annotations, you should do the following: (1) add a colon and a space after the variable’s or constant’s name, and (2) indicate the type you want to use.

In the following example, you will add a type annotation to a variable named “bootMessage,” to show that only “string” values can exist within that variable.

var bootMessage: string

Swift reads the colon as “of type.” Thus, you can read the code given above as:

“Create a new variable, call it bootMessage, and make sure that it can only hold String values.

Important Note: In real life, you will rarely add type annotations into your Swift codes. If you’ll set the initial value of a variable or constant, Swift will be able to detect the data type that can be stored into that variable or constant.

How to Create Names for Variables and Constants?

In Swift, you can use almost any character (even Unicode ones) when naming variables and constants. Here are some examples:

let = “infinite”

let à = “diamond”

let= “trademark”

However, you cannot add arrows, math symbols, private-use Unicode characters, box-drawing characters, or line-drawing characters to the names of your variables/constants. You may include numbers, but you cannot use them to start the name. Thus, “sixdogs” is valid while “6dogs” isn’t.

After specifying the data type of a variable or constant, you can no longer use its name for other variables/constants or set it to hold other data types.

Swift allows you to replace the value of a variable with another one. You just have to make sure that the new value is compatible with the variable you are working on. In the following example, the value of “color” is changed from “blue” to “red.”

var color = “blue”

color = “red”

// color is now “red”

How to Print Variables and Constants?

You may use the “println” function to print the value of a variable or constant. Here is an example:

println(color)

// prints “red”

Basically, println is a Swift function that can print values to the correct output. For instance, if you are using Xcode (i.e. a programming tool for iOS developers), the println function will print the output to the console pane of your Xcode screen.

This function will print String values passed to it. For example:

println(“This is an excellent book”)

// prints “This is an excellent book”

Swift employs string interpolation to help constants and variables in holding long strings. String interpolation also assists the computer language in replacing the value of any variable or constant. The following example will show you how to use string interpolation:

println(“My favorite color is \(red)”)

// prints “My favorite color is red”

Comments

In the Swift language, you can add non-executable texts into your codes using “comments.” The compiler of this language ignores comments during code compilation.

The comments in this language are similar to that of C. You should begin single-line comments using two forward-slashes (i.e. //). Here’s an example:

// this comment is awesome

To write comments that span multiple lines, add /* and */ at the beginning and end of your statements, respectively. The example below will serve as a guide:

/* this awesome comment

needs

four

lines */

Unlike C, Swift allows you to place multiline comments within existing multiline comments. Programmers refer to this process as “nesting.” To nest comments, begin the first comment block then start the next block inside the current one. Here, you must close the nested comment before closing the first comment. The sample code below will illustrate this idea:

/* I’m the first comment

/* I’m the second comment*/

You’re looking at the first comment again */

Semicolons

You don’t have to use semicolons to end each of your Swift statements. You only need to add a semicolon when writing several statements within a single line. Here’s an example:

let color = “blue”; println(color)

// prints “blue”