Swift Variables

In swift, variables are used to store and manipulate a data by performing different operations based on our requirements. We can store different types of data in variables like integers, strings, characters, bool, etc.

 

Whenever we create a variable in swift programming language, automatically the required space is reserved in computer memory for that variable and the space allocation in memory is varied based on the type of variable like integer or float or double, etc.

 

Swift supports us to declare the different types of variables like Array, Dictionaries and etc. Following are the basic and most frequently used data types with variables in swift programming language.

 

Data TypeDescriptionExample
Integer Int or UInt stand for Integer and used for numbers like 1, 2, 3, 4, 5. We can use Int32, Int64 to define 32 or 64-bit signed integer, whereas UInt32 or UInt64 to define 32 or 64-bit unsigned integer variables. For example, 345566 and -345566.

100

Float Float stand for floating value and it is used to represent numbers with fraction value like 245.344 and etc. It is used to hold the numbers with larger or smaller decimal points like 3.14, and -455.3344.

3.14

-455.3344

Double Double stands for Double means if we want to allocate the large number with fraction value then we use Double as a float data-type. We can use it as a 64-bit floating-point number and Double is more precise than Float. For example, 345.344544, and -4554.3455543.

345.344544

-4554.3455543

Bool Bool represent only two values either TRUE or FALSE. We can use Bool to check whether certain conditions met or not.

let i = 10

if i> 20 {

// your code

}

String String is a combination of Characters. We can define string data type by adding double quotes to our text like “Hello World”.

""

"welcome to tutlane"

Character It represent a single alphabet character like “H”, “e”, “L”, “L”, “o”. “H”
Optional It represent a variable can hold value or no value.  

Swift Variable Declaration

In any programming language first we need to declare variables before they used it in our applications. In swift we use “var” keyword to define variables and we can change the values of variables in application based on our requirements.

 

Following is the syntax of declaring the variable in a swift programming language.

 

var <VariableName> = <Value>

Now write the code like as shown below in Xcode Editor and see the result.

 

var str = "Welcome Tutlane"

print(strr)

In above code first line tells the compiler that we declared a variable “str” and assigns its value, by doing this compiler will allocate the required space in our computer memory to hold the variable value and in the second-line, we will print “str” variable by its name.

 

Following is the result of the above swift variable declaration program.

 

Welcome Tutlane

Swift Type Annotations

In swift by defining type annotation while declaring a variable, we can easily know that what kind of values the variable can store.

 

We can define variable type annotation by placing a colon after the variable name, followed by a space and followed by the name of the type to use.

 

Following is the syntax of defining type annotation to the variable in Swift.

 

var varName: <data type> = <initial value>

Following is the simple example of defining type annotation to the variable in our Xcode Editor.

 

var str: String = "Welcome Tutlane"

print(strr)

If you observe the above example we defined variable “str” with its data type (String). The above swift program will return value like as shown below.

 

Welcome Tutlane

Note: when we declare a variable with type annotation then it’s mandatory to mention initial value of variable correspond to its data type.

 

Following is another example of defining the variable with its data-type.

 

var PI: Float = 3.14

print(PI)

The above swift program will return the result like as shown below.

 

3.14

Swift Naming Variables

In swift, variable names can contain almost all the characters like strings, numbers, underscore and including Unicode characters but it should not contain any whitespace characters, mathematical symbols, arrows, and invalid Unicode code points.

 

The variable name should not start with numbers but it can contain the numbers elsewhere in the name. Following is the simple example of naming variables in a swift programming language.

 

var val123 = "Welcome"

print(val123)

 

var _sampleVal = "Tutlane"

print(_sampleVal)

 

var 你好= "你好世界"

print(你好)

If you observe above example we used simple Unicode characters also as variable names. For the above example, the swift playground will return a result like as shown below.

 

Welcome

Tutlane

你好世界

Swift Variable Concatenation & Interpolation

In swift we can combine two variables into a single variable for that we have two ways, one is we can concatenate it and the second is to interpolate variables with parenthesis.

 

Following is the simple example of concatenating two variables into a single variable using swift.

 

var var1 = "Welcome"

var var2 = "Tutlane"

 

var result = var1 + var2

print(result)

The swift programming playground will return the result like as shown below.

 

Welcome Tutlane

Now we will see how to use interpolation to combine two variables with examples. Following is the simple example of using interpolation to combine two variables in swift.

 

var var1 = "Welcome"

var var2 = "Tutlane"

print("/(var1) /(var2)")

The above swift program will return result like as shown below.

 

Welcome Tutlane

Swift Print Variables

In swift, we can print variables using a function called “print”. Following is the simple example of printing variables.

 

var var1 = "Welcome to "

var var2 = "Tutlane"

 

var result = var1 + var2

print(result)

The swift programming playground will return the result like as shown below.

 

Welcome to Tutlane

This is how we can define, declare and use variables in a swift programming language based on our requirements.