What are Variables in Swift?

In Swift, variables are used to store data that can change during the execution of a program. Variables allow you to label and manipulate data easily, making your code more readable and maintainable.

In this tutorial, you will learn how to declare variables, and how to use them in Swift programs to read values from variables, update values in variables, with detailed examples.


Declaring Variables

To declare a variable in Swift, use the var keyword followed by the variable name and optional type annotation. For example:

var variableName: DataType = value

If the type of the variable can be inferred from the value, the type annotation is optional.


Example 1: Declaring and Using Variables

In this example, we will declare and use variables of different data types like integer, string, and boolean, and read the values in variable, and print them to output.

File: main.swift

</>
Copy
var age: Int = 25
var name: String = "Alice"
var isStudent: Bool = true

print("Name: \(name)")
print("Age: \(age)")
print("Is Student: \(isStudent)")

Explanation:

  1. age is declared as an integer variable and assigned the value 25.
  2. name is declared as a string variable and assigned the value "Alice".
  3. isStudent is declared as a boolean variable and assigned the value true.
  4. The print statements output the values of the variables using string interpolation (e.g., \(variableName)).

Output:

Output for Example to Declaring and Using Variables in Swift

Example 2: Updating Variable Values

In this example, we will initialise a variable and then update the variable by reassigning with another value, and print the updated value to output.

File: main.swift

</>
Copy
var score = 10
print("Initial score: \(score)")

// Updating the value of score
score = 20
print("Updated score: \(score)")

Explanation:

  1. score is declared and initialized with the value 10.
  2. The value of score is updated to 20.
  3. The updated value is printed, showing that the variable can change its value during execution.

Output:

Output for Example to Updating Variable Values in Swift

Example 3: Combining Variables and Constants

In Swift, if you want a value to remain constant (unchangeable), use the let keyword instead of var.

In the following program, we will demonstrate how to use constants and variables together.

File: main.swift

</>
Copy
let base = 10
var height = 5

let area = base * height
print("Area of the rectangle: \(area)")

Explanation:

  • base is declared as a constant with a value of 10.
  • height is declared as a variable with a value of 5.
  • The area is calculated by multiplying base and height. Since base is constant and height can change, this setup provides flexibility while preserving immutability for base.

Output:

Output for Example to Combining Variables and Constants in Swift

Type Inference in Swift

Swift can often infer the type of a variable or constant from its initial value, so you can omit the type annotation:

File: main.swift

</>
Copy
var message = "Hello, Swift!" // Swift infers that message is of type String
var number = 42 // Swift infers that number is of type Int

print("Message: \(message)")
print("Number: \(number)")

Output:

Output for Example to Type Inference in Swift

Conclusion

Variables and constants are fundamental concepts in Swift. Use var for values that can change and let for values that should remain constant.