Swift Tutorial Swift References

Swift - Variables



Variable is a given name to a reserved memory location. When a variable is created in the program, it reserves some space in the memory to store value(s) and the interpreter allocates memory for the given variable based on its datatype. Value(s) is stored in the variable by assigning different datatypes to it like number, string and sequences, etc.

Swift supports the following basic and most frequently used data types:

  • Int − An integer type, which has the same size as the current platform's native word size:
    • On a 32-bit platform, Int is the same size as Int32.
    • On a 64-bit platform, Int is the same size as Int64.
    Int8, Int16, Int32, Int64 can be used to represent 8 Bit, 16 Bit, 32 Bit, and 64 Bit forms of signed integer. For example, -10, 10, and 50.
  • UInt − An unsigned integer type, which has the same size as the current platform's native word size:
    • On a 32-bit platform, UInt is the same size as UInt32.
    • On a 64-bit platform, UInt is the same size as UInt64.
    UInt8, UInt16, UInt32, UInt64 can be used to represent 8 Bit, 16 Bit, 32 Bit and 64 Bit forms of unsigned integer. For example, 10, and 50.
  • Float − Represents a 32-bit floating-point number and numbers with smaller decimal points. For example, 3.14159, 0.1, and -273.15.
  • Double − Represents a 64-bit floating-point number and used when floating-point values are very large. For example, 3.14159, 0.1, and -273.15.
  • Bool − Represents a Boolean value which is either true or false.
  • String − A series of characters. For example, "Hello, World".
  • Character − A single-character string literal. For example, "A"
  • Optional − Represents a variable that can hold either a value or no value.
  • Tuples − Used to group multiple values in single compound value. The values within a tuple can be of any type and don't have to be of the same type as each other.

Swift also provides three primary collection types, Array, Set, and Dictionary, which are discussed in later sections. In this section, we are going to discuss how to declare and use various types of variables in Swift programming.

Swift Variable Declaration

A variable declaration tells the compiler where and how much to create the storage for the variable. In Swift, a variable is declared using var keyword. Consider the example below:

var i = 10
var j = "Hello"

print(i)
print(j)

The output of the above code will be:

10
Hello

Swift Type Annotations

A type annotation can be provided while declaring a variable. The syntax for using type annotation is given below:

var variableName : <data type> = <optional initial value>

Please note that, when not using type annotation, then it is mandatory to provide an initial value for the variable. With type annotation, a variable can be declared without initial value. Consider the example below:

var i:Int = 10
var j:String
j = "Hello"

print(i)
print(j)

The output of the above code will be:

10
Hello

Variable Name

There are some reserved keywords in Swift which cannot be used as variable name. Along with this, rules for creating Swift variable name are listed below:

  • It must start with a letter or the underscore character
  • It cannot start with a number.
  • It can only contains alpha-numeric characters and underscores (A-Z, a-z, 0-9, and _ ).

Please note that Swift is a case-sensitive language. Hence, variables in Swift are also case-sensitive. A simple or Unicode characters can be used to name a variable.

Print Variable

The value of the variable can be printed on the screen, or other standard output device using print() function. A variable can be interpolated wrapping it in parentheses and escape it with a backslash, \(variable). Consider the following example:

var person = "John"
var age = 25

print("\(person) is \(age) years old.")

The output of the above code will be:

John is 25 years old.

Alternatively, it can also be achieved by using comma , operator which concatenates string values of variables with a whitespace.

var person = "John"
var age = 25

print(person, "is", age, "years old.")

The output of the above code will be:

John is 25 years old.

The plus + character can also be used but with some limitation. It combines two or more variables of same datatypes. With string datatype, it returns concatenated variables and with number datatypes it returns sum of the variables. With mixed datatypes, it will raise an exception.

var person = "John"
var city = "London."

print(person + " lives in " + city)

The output of the above code will be:

John lives in London.

It is possible to use comma , and plus + characters inside print() function at the same time to get the desired result.

var x = 25
var y = 10

print("sum of", x,"and", y,"is", x+y)

The output of the above code will be:

sum of 25 and 10 is 35