What Variable and Constant in Swift?

Variable, Constant and Naming Rules

Tested on: Xcode 12.2, Swift 5.3

Variables and Constants are used to store data on your computer or phone. Just like the box shown in the image below, when these boxes store a value of 1 in each box but there is a difference between variable and constant although both store data.

Constant and Variable in Swift
  1. Variable
  2. Constant
  3. Declaring with Data Type
  4. Checking the inferred type
  5. Naming Rules and Conversion

1. Variable

As mention above, variable and constant is used to store data but what is the difference between variable and constant?

As you see from the image above, the box in the variable does not have a cap meaning you can take out the value inside and change it while the constant cannot. For example, in a post, the number of "likes" can be increase or decrease. Therefore, you should store them as a variable because the data can be changed.

To create a variable, we use the keyword, var and we use an assignment operator = to store the value. The code below means that the value 1 is part of or assigned tovariableBox.

Swift Code :

var variableBox = 1

Changing the value of a variable

To change the value inside the box (variable), we can use the variable name and assignment operator and assign it to a new value. You do not need to use the var keyword when changing to a new value.

Swift Code :

variableBox = 2

2. Constant

As mention above, the value in the constant cannot be changed (immutable). Once you create a constant, the value is always fixed and sealed. In other words, you can only put something into the box of a constant and you cannot change it afterwards.

What is it for? and why can't I just create a variable? Since constant is troublesome! Constant is useful and it is used as a safety feature to prevent you from accidentally changing a value that should not be changed. It's one of the features of Swift as compared to other languages.

To create a constant, we use a let keyword.

Swift Code :

let constantBox = 1

If you try to change the value in the constant, you will get an error message.

Swift Code :

let constantBox = 1

Output :

Left side of mutating operator isn't mutable: 'constantBox' is a 'let' constant

3. Declaring with Data type

Above we declare our variable and constant without declaring its data types. Is it know as Type inference. ( Refer to Data type to understand more about Data type ). But why do we need to declare data type since we can assign it directly like that one above? Because sometimes, Swift compiler is not able to interpret the data type correctly and so, you have to tell the compiler what data type is it.

To do that you use a colon and then its data type e.g :int,:float.

Swift Code :

let constantBox:Int = 1

4. Checking the inferred type

As mentioned, Swift able to deduce the data type well and you do not need to tell them all the type. It is useful to check the inferred data type of a variable or constant. Hold down Option key on the variable or constant name.

Constant and Variable in Swift

5. Naming Rules

You can name anything to your variable or constant but there are some rules to follows :

Things you cannot do:

Swift Code :

// You cannot start the name with numbers
let 1name = 1000
// You cannot start the name with special letter
let @name = 1000
// You cannot include special letter in your name
let na/me = 1000
// You cannot use keywoord as a name.
let if = 1000

Things you can do:

Swift Code :

// You can use small or capital letter
let NaMe = 1000
// You can use number inside your name but cannot start with number
let name2 = 1000
let na2me = 1000
// You can use underscore
let _name_ = 1000

Naming Conversion

In Swift, the common and conversion way of naming your variable and constant is to use the camel case. What is Camel Case?

Start your name with a lowercase letter and if the name is made up of multiple words, join them by using the word starting with an uppercase letter such as multiple.

Swift Code :

let myNameIs = "Wes"