Swift Tutorial: Introduction 02 - Variables and Constants

Written by Paul Napier on . Posted in Swift Tutorials: Introduction

Hello and welcome to the second tutorial in the introduction to swift programming brought to you by MadApper. Once again I am Paul Napier and in this lesson we will be covering the very first and possibly most important aspects of the language: variables and constants.


If you have come from an Objective C background these lines of code will look familiar to you:

NSString *string = @”Hello, world”;
NSArray  *array = [[NSArray alloc]init];
int integer = 10;

Or if you come from a Java background then you will be familiar with these:

String string = “Hello, world”;
ArrayList<String> array = new ArrayList<String>();
Int integer = 10;

What you are doing here is creating variables: a string, an array, and an integer. Don’t worry too much about these at the minute, as we will cover each of these I much greater depth later on in the tutorials.

In Swift, the process for creating variables is even simpler:

var variable = 10
let constant = 20

You may have noticed that I have created two elements here, a variable and a constant. For the sake of brevity, a variable is something whose value can be changed, where as a constant is something whose value cannot change once it has been set.

So if I were to change the variable to 30, this would be possible:

variable = 30

Whereas, if I try to change the value of the constant, I am given an error:

constant = 40

swift constant reassignment error 

Now you can see that  even though I have not defined the type or class of the variable the compiler in the playground has seen that I am assigning an integer value to this and has picked it up. This is called implicit assignment, and if I now try to assign the variable as a String, you can see that an error is thrown.

variable = "string"

swift variable conversion error

However, there may be times where you want to explicitly define the type or class of the variable, and I will cover examples of this in more depth as we progress through the lessons. In these instances, you supply a type annotation.

 var variable2: Int = 50

So, before we finish there are just a couple of final points on constants and variables:

1) You can name them with just about any Unicode character. However, constant and variable names cannot contain mathematical symbols, arrows, private-use Unicode code points, or line- and box-drawing characters. Nor can they begin with a number, although numbers may be included anywhere else in the name.

2) It is common and good practice when naming variables to start them with a lowercase letter, so as not to confuse them with class names which usually start with capital letters. We’ll cover classes in much greater depth later.

 3) Finally, once you’ve declared a constant or variable of a certain type, you can’t redeclare it again with the same name. Nor can you change a constant into a variable or a variable into a constant. 

OK so now you have a fair understanding of variables and constants. In the next lesson we will cover a few syntactical points that are useful to know before you start coding. 



0 #1 Anemonalove 2017-07-19 12:39
Hello guys! Who wants to meet me? I have profile at,
we can chat, you can watch me live for free, my nickname
is Anemonalove , here is my photo:

Add comment

Security code