Swift Variables

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:-

or

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:-

Output:-

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:-

Example:-

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:-

Example:-

Output:-

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:-

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:-

Output:-

In this tutorial we have learn about the Swift Variables and its application with practical example. I hope you will like this tutorial.