Constants and Variables in Swift

Swift Language

In this article we will learn about variables and constants in the Swift programming language. Swift uses variables to store and refer to values by an identifying name. Swift also makes extensive use of variables whose values can’t be changed.

These are known as constants, and are much more powerful than constants in C. Constants are used throughout Swift to make code safer and clearer in intent when you work with values that don’t need to change.

Swift is a “type-safe language“, which means the language helps you to be clear about the types of values your code can work with. If part of your code requires a String, type safety prevents you from passing it an Int by mistake. Likewise, type safety prevents you from accidentally passing an optional String to a piece of code that requires a non-optional String. Type safety helps you catch and fix errors as early as possible in the development process.

Declaring Constants and Variables

Like all other programming languages, constants and variables must be declared before they’re used.

Declare Keyword:

  • Constants: let
  • Variables: var

Use variables only for storing values that need to be able to change. If a stored value in your code won’t change, always declare it as a constant with the let keyword.

Example:

let minValue = 1000
var flag = true

You can understand it simply as follows: The minValue constant represents the minimum value of 1000 and this value never changes. The flag variable is declared as a boolean with a value of true, it can be assigned a new value when programming.

You can declare multiple constants or multiple variables on a single line, separated by commas. Example:

let minValue = 1000, maxValue = 9999
var x = 0.0, y = 0.0

Type Annotations

You can explicitly declare the data type of the variable and the constant according to the following syntax:

var <Variable Name>: <Type>
let <Variable Name>: <Type>

Example: You can declare a string type variable as follows

var website: String
website = "izziswift.com"

Declare the string type variable and name it website. Assign the value website variable “izziswift.com

You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name:

var x, y, z: String
let R, G, B: Int32

Naming Constants and Variables

Swift’s Constant and variable names can’t contain whitespace characters, mathematical symbols, arrows, private-use Unicode scalar values, or line- and box-drawing characters. Constant and variable names can contain almost any character, including Unicode characters.

Example:

let π = 3.14
var 名前 = "ダンニャミン"
let ? = "dog"

In Swift, unlike a variable, the value of a constant can’t be changed after it’s set. Attempting to do so is reported as an error when your code is compiled:

//Change the value of an existing variable
var website: String
website = "izziswift.com"
website = "any"

//Attempting to do so is reported as an error
let minValue = 1000
minValue = 300
The value of a constant can’t be changed after it’s set.
The value of a constant can’t be changed after it’s set.

Printing Constants and Variables

In Swift, the print(_:separator:terminator:) function is a global function that prints one or more values to an appropriate output.

Example:

var website: String
website = "izziswift.com"
print(website)

Run this code with Xcode 10.X:

Printing Constants and Variables
Printing Constants and Variables

By default, the function terminates the line it prints by adding a line break. To print a value without a line break after it, pass an empty string as the terminator.

print(someValue, terminator: "")

Swift uses string interpolation to include the name of a constant or variable as a placeholder in a longer string, and to prompt Swift to replace it with the current value of that constant or variable. Wrap the name in parentheses and escape it with a backslash before the opening parenthesis:

var website: String
website = "izziswift.com"
print("Welcome to \(website)")

Run this code with Xcode 10.X:

String interpolation
String interpolation