Skip to main content

Swift Variables

Variable is a name of identifier which is capable to hold some values. Swift Programming is provide val and var keyword to declare variable in our program. Internally name of variable are work with memory location, This memory is allocated by compiler, when executing the program.

Swift Variable

Programer are use this memory by assign the value of variable and retrieve variable value. Variable type is decide which type of value it will store. Normally variable are use to hold primitive and user defined data type values. We can declare variable in using of this syntax.

modifier_type variable_name : data_type = variable_value

Type modifier is describe behaviour of variable, let-keyword are used to declare constant variable. and var keyword is use to define non-constant variable. After that mention the name of variable, which is user define. Colon and data type is describe the value type.

//define constant variable (can't be modified)
let months=12 //immutable variables 

//Mutable variables (possible to modified)
var age=12     //according to value that is an integer variable  
var name:String //name is an string variable
var population:Double //population is an double type of variable
var status:Bool   //status is boolean type

Note that in a declaration of variable when assign of value there is no need to provide data type in variables. In this case compiler are provide variable type when will running of program.This type of variable is called inferred variables. But in other case there is compulsory, when not assign initial value to variables.

By using of comma, we can define similar data type variables into single line statements.

//Define Integer variable
var x = 10 , y = 20 , z = 30 

//Define multiple string variables
var name = "Coder" , address = "Top-level Anywhere"

In this example provide value of variable so compiler are understand its type. We can also declared of multiple variable in following way.

//Define Integer variable
var x , y, z : Int

//Define multiple string variables
var name , address : String

The Main advantage of variable are that is provide dynamic environment they are capable to control instructions and operation when executing program.

Difference between let and var

var is a keyword which are used to create a variable in swift programming. The value of this type of variable can be change at any time during program execution. Therefore That is called mutable variable.

var variables : is Mutable that are modified.

var salary:Double = 1000

print("Before salary : \(salary)")
salary=salary+5000
print("After salary : \(salary)")

Output

Before salary : 1000.0
After salary : 6000.0

val keyword are used to create constant variable of any data type. that value are constant, So variable cannot change this value when that is assigned. So this type of variable capable to get value ones and can be use so many times, therefore that is called immutable variable.

//Define constant variables
let days:Int //okay days is an constant variable for integer type

days=365 //okay - Valid for assign first time

print("days: \(days)")

let maxSpeed=220 //okay - that is an constant integer variable

//never modified
// maxSpeed=250 //not possible to change

Note that when assign a value of variable they are optional to provide their data type. But when not assigning initial values, Then compulsory to mention data type. In this example declare a constant variable by using let keyword but note that not assigned any value. In this case data type is required to data type which type of constant variable are create.

Swift compiler are provide an approchinity to constant variable, which is not have assign initial value. Then we can assign that initial value after its declaration. In simple word, constant variable set initial value only once. Swift compiler optimize the memory used for constant variables.

Get the variable data type

We can get variable data type of using inbuilt function type(of:variable).

var a=10
var b="Programming"
var c:Float=12.45
print(type(of : a)) //Int
print(type(of : b)) //String
print(type(of : c)) //Float

Output

Int
String
Float

Find the variable size

//Find the size of variable value and literal data type
let x=900
let size = MemoryLayout.size(ofValue: x)
print(size)

Output

8

stride example

var x = "ABCDEF"
var size = MemoryLayout.size(ofValue: x)
print(size)//24

Output

24

alignment are return default alignment memory to variable

let x = "ABCDEF"
var size = MemoryLayout.size(ofValue: x)
print(size)//24

size = MemoryLayout.alignment(ofValue: x)
print(size)//8

Output

24
8

Convert string to integer

var textData :String = "10"
let myInt: Int = Int(textData)!
print(myInt) //Display intger

Output

10

When declaring a variable in swift programming there are not possible to modify their data type. And there are not possible to declare two or more similar name of variable inside a same scope and context.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment