Swift - Variables



What are Variables in Swift?

A variable provides us with named storage that our programs can manipulate. Each variable in Swift has a specific type, which determines the size and layout of the variable's memory. A variable can store the range of values within that memory or the set of operations that can be applied to the variable.

Variables are statically types means once they are declared with a certain type, then their type cannot be changed, only their value can be changed.

Declaring Swift Variables

A variable declaration tells the compiler where and how much to create the storage for the variable. They are always declared before their use and they are declared using the var keyword.

Syntax

Following is the syntax of the variable −

var variableName = value 

Example

Swift program to demonstrate how to declare variables.

import Foundation

// Declaring variable
let number = 42
print("Variable:", number)

Output

Variable: 42

We can also declare multiple variables in a single line. Where each variable has its values and is separated by commas.

Syntax

Following is the syntax of multiple variables −

var variableA = value, variableB = value, varaibleC = value  

Example

Swift program to demonstrate how to declare multiple variables in a single line.

import Foundation

// Declaring multiple variables
var variableA = 42, variableB = "TutorialsPoint", variableC = 3.3 

print("Variable 1:", variableA)
print("Variable 2:", variableB)
print("Variable 3:", variableC)

Output

Variable 1: 42
Variable 2: TutorialsPoint
Variable 3: 3.3

Type Annotations in Swift

Type annotation is used to define what type of value should be stored in the variable at the time of declaration. While declaring a variable we can specify type annotation by placing a colon after the variable name followed by the type.

Type annotation is rarely used if we provide an initial value at the time of declaring a variable because Swift will automatically infer the type of the variable according to the assigned value.

Syntax

Following is the syntax of type annotations −

var variableName : Type = Value

Example

Swift program to demonstrate how to specify type annotation.

import Foundation

// Declaring variable with type annotation
var myValue : String = "Swift Tutorial" 
print("Variable:", myValue)

Output

Variable: Swift Tutorial

We can also define multiple variables of the same type in a single line. Where each variable name is separated by a comma.

Syntax

Following is the syntax of multiple variables −

let variableA, variableB, varaibleC : Type

Example

Swift program to demonstrate how to specify multiple variables in single-type annotation.

import Foundation

// Declaring multiple variables in single-type annotation
var myValue1, myValue2, myValue3 : Int

// Assigning values 
myValue1 = 23
myValue2 = 22
myValue3 = 11

print("Variable Value 1:", myValue1)
print("Variable Value 2:", myValue2)
print("Variable Value 3:", myValue3)

Output

Variable Value 1: 23
Variable Value 2: 22
Variable Value 3: 11

Naming Variable in Swift

Naming a variable is a very important part at the time of declaration. They should have a unique name. You are not allowed to store two variables with the same name if you try to do you will get an error. Swift provides the following rules for naming a variable −

  • Variable names can contain any character including unicode characters. For example, var 你好 = "你好世界".

  • A variable name should not contain whitespace characters, mathematical symbols, arrows, private-se Unicode scalar values, or line and box drawing characters.

  • The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. For example, var myValue = 34.

  • Upper and lowercase letters are distinct because Swift is case-sensitive. For example, var value and var Value are two different variables.

  • Variable names should not begin with a number.

  • You are not allowed to re-declare a variable with the same name. Or cannot change into another type.

  • You are not allowed to change a variable into a constant or vice versa.

  • If you want to declare a variable name the same as a reserved keyword, then use backticks(') before the name of the variable. For example, var 'break = "hello".

Printing Swift Variable

You can print the current value of a variable using the print() function. You can interpolate a variable value by wrapping the name in parentheses and escaping it with a backslash before the opening parenthesis.

Example

Swift program to print variables.

import Foundation

// Declaring variables 
var varA = "Godzilla"
var varB = 1000.00

// Displaying constant
print("Value of \(varA) is more than \(varB) million")

Output

Value of Godzilla is more than 1000.0 million
Advertisements