Swift Variables
A variable is a name given to a particular area in memory, that stores a value, which can later be reused. Variable names in Swift can contain letters, numbers and underscores, along with some Unicode characters, but can't begin with a number. They are also case sensitive. It is possible to declare a variable with or without specifying the type of data that it can store, such as string, integer or floating-point number. Where no data type is specified, Swift allocates a type based on the data that is assigned to it.
When creating a variable, it takes the following format. The equals sign between the variable name and value is known as an assignment operator and will be discussed further in the section on operators.
// Variable without a data type specified. var variable_name = variable_value // Variable with a data type specified. var variable_name:variable_type = variable_value
Where a type is specified, the variable doesn't need to be assigned a value when it is declared.
// Variable with a data type specified. var variable_name:variable_type // Assign a value separately, after the variable has been declared. variable_name = variable_value
Below are some example variables, the last of which is a Boolean variable, that can either be ‘true’ or ‘false’.
// Integer variables. var exampleInteger1 = 5 var exampleInteger2:Int = 10 // Floating point or decimal number variables. var exampleFloat1 = 5.0 var exampleFloat2:Float = 10.0 // String variables. var exampleString1 = "This is a string" var exampleString2:String = "This is another string" // Boolean variables. var exampleBoolean1 = true var exampleBoolean2:Bool = false
One thing to note is that variable names must not be the same as reserved words in Swift. Reserved words have a specific meaning and cannot be used for anything else. They are shown in blue on the preceding and following pages.
The values of variables can be displayed, for example, in the console pane of an Xcode Playground, using the 'print' function.
print(exampleInteger1) print(exampleInteger2) print(exampleFloat1) print(exampleFloat2) print(exampleString1) print(exampleString2) print(exampleBoolean1) print(exampleBoolean2)
This will display the following in the console pane.
5 10 5.0 10.0 This is a string This is another string true false
If the value of a variable needs to be displayed as part of a longer string, Swift uses string interpolation to achieve this.
print("Value of exampleInteger1: \(exampleInteger1)")
print("Value of exampleInteger2: \(exampleInteger2)")
print("Value of exampleFloat1: \(exampleFloat1)")
print("Value of exampleFloat2: \(exampleFloat2)")
print("Value of exampleString1: \(exampleString1)")
print("Value of exampleString2: \(exampleString2)")
print("Value of exampleBoolean1: \(exampleBoolean1)")
print("Value of exampleBoolean2: \(exampleBoolean2)")
The output produced is shown below.
Value of exampleInteger1: 5 Value of exampleInteger2: 10 Value of exampleFloat1: 5.0 Value of exampleFloat2: 10.0 Value of exampleString1: This is a string Value of exampleString2: This is another string Value of exampleBoolean1: true Value of exampleBoolean2: false
All the the examples above have declared and initialised each variable on a separate line. It is possible to declare and initialise multiple variables on the same line.
var exampleInteger1 = 5, exampleFloat1 = 5.0, exampleString1 = "This is a string"
Here, an integer, floating point and string variable are all declared and initialised on the same line using the 'var' keyword.
It is also possible to declare multiple variables by explicitly specify the type.
var exampleInteger1, exampleInteger2, exampleInteger3:Int