In this tutorial you will learn about the Swift Variables and its application with practical example.
Swift Variables
Variables is an identifier used to refer memory location in computer memory that holds a value for that variable, this value can be changed during the execution of the program. When you create a variable in Swift, this means you are allocating some space in the memory for that variable. The size of memory block allocated and type of the value it holds is completely dependent upon the type of variable.
Rules for naming a variable –
Constant and variable names cannot contain whitespace characters, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters.
- Variable name can consist of letter and alphabets.
- Keywords are not allowed to use as a variable name.
- Blank spaces are not allowed in variable name.
- Mathematical symbols, arrows, Unicode code points are not allowed.
Recommendation :- Variable name must be readable and should be relative to its purpose.
Declaring Variables In Swift
In Swift, a variables must be declared before they are used. In Swift, variables are declared using the var keyword followed by variable name that you want to declare, a colon (:) and then the data type you want to hold in that variable.
Syntax:-
1 |
var <name>: <type> |
or
1 |
var <name>:<type> = <expression> |
Variable assignment In Swift
The assignment operator (=) is used to assign values to a variable, the operand in the left side of the assignment operator (=) indicates the name of the variable and the operand in the right side of the assignment operator (=) indicates the value to be stored in that variable.
Example:-
1 2 3 4 5 6 7 |
var myvar:String myvar = "Hello, World!" var marks:Float = 80.5 var age:Int = 25 print(myvar) print(marks) print(age) |
Output:-
1 2 3 |
Hello, World! 80.5 25 |
Here, we have three assignment statements. In first statement variable “myvar” is assigned an string value “Hello, World!”. Similarly, in the second statement a floating point value 80.5 is assigned to variable “marks” and 25 is a integer assigned to the variables “age”.
Type Annotations
In Swift, while declaring a variable you can optionally provide a type annotation, to suggest type of values the variable can hold.
Syntax:-
1 |
var <name>:<type> = <expression> |
Example:-
1 |
var myvar: String |
Here, type annotation for a variable called “myvar” is provided that indicate the variable can hold String values.
Multiple assignments
In Swift, it is possible to define multiple variables of same type in a single statement separated by commas, with a single type annotation after the final variable name as following-
Example:-
1 |
var age, marks: Int |
Example:-
1 2 |
var x = 1, y = 2, z = "Hello, World!" print(z) |
Output:-
1 |
Hello, World! |
Type Inference
Swift is a type inferred language, which allows us to drop the type annotation from the declaration. In Swift, compiler automatically infer(know) the type of data we want to store based on the initial value we assign.
Example:-
1 |
var myvar = "Hello, World!" |
Here, type annotation is not used in the declaration but the Swift compiler simply infer the type automatically for us.
Printing Variables
In Swift, print() function is used to print the current value of a variable. String interpolation can be done by wrapping the variable name as placeholder in parentheses and escape it with a backslash before the opening parenthesis. Swift compiler replaces placeholder with the current value of corresponding variable.
Example:-
1 2 3 4 5 6 7 8 9 |
// First variable var minvalue = 0 // Second variable var maxvalue = 100 print("1st var's value is \(minvalue) and 2nd var's value is \(maxvalue)") |
Output:-
1 |
1st var's value is 0 and 2nd var's value is 100 |