Swift - Constants



What is Constant in Swift?

Constants refer to fixed values that a program may not alter during its execution. Constants can be of any data type like an integer, float, character, double, or a string literal. There are enumeration constants as well. They are treated just like regular variables except for the fact that their values cannot be modified after their definition.

Declaring Swift Constant

Constants are used to store values that won’t change throughout the whole execution of the program. They are always declared before their use and they are declared using the let keyword.

Syntax

Following is the syntax of constant −

let number = 10

Example

Swift program to demonstrate how to declare constants.

import Foundation

// Declaring constant
let constA = 42
print("Constant:", constA)

Output

Constant: 42

If we try to assign value to a constant, then we will get an error just like in the below example −

import Foundation

// Declaring constant
let constA = 42
print("Constant:", constA)

// Assigning value to a constant
constA = 43
print("Constant:", constA)

Output

main.swift:8:1: error: cannot assign to value: 'constA' is a 'let' constant
constA = 43
^~~~~~
main.swift:4:1: note: change 'let' to 'var' to make it mutable
let constA = 42
^~~
var

We can also declare multiple constants in a single line. Where each constant has its values and is separated by commas.

Syntax

Following is the syntax of multiple constants −

let constantA = value, constantB = value, constantC = value

Example

Swift program to demonstrate how to declare multiple constants in a single line.

import Foundation

// Declaring multiple constants
let constA = 42, constB = 23, constC = 33
print("Constant 1:", constA)
print("Constant 2:", constB)
print("Constant 3:", constC)

Output

Constant 1: 42
Constant 2: 23
Constant 3: 33

Type Annotations with constants

Type annotation is used to define what type of value should be stored in the constant at the time of declaration. While declaring a constant we can specify type annotation by placing a colon after the constant name followed by the type.

Type annotation is rarely used if we provide an initial value at the time of declaring a constant because Swift will automatically infer the type of the constant according to the assigned value.

For example, let myValue = "hello", so the type of myValue constant is String because we assigned a string value to the constant.

Syntax

Following is the syntax of type annotations −

let constantName : Type = Value

Example

Swift program to demonstrate how to specify type annotation.

import Foundation

// Declaring constant with type annotation
let myValue : String = "hello" 
print("Constant:", myValue)

Output

Constant: hello

We can also define multiple constants of the same type in a single line. Where each constant name is separated by a comma.

Syntax

Following is the syntax of multiple constants −

let constantA, constantB, constantC : Type

Example

Swift program to demonstrate how to specify multiple constants in single-type annotation.

import Foundation

// Declaring multiple constants in single-type annotation
let myValue1, myValue2, myValue3 : String 

// Assigning values 
myValue1 = "Hello"
myValue2 = "Tutorials"
myValue3 = "point"

print("Constant Value 1:", myValue1)
print("Constant Value 2:", myValue2)
print("Constant Value 3:", myValue3)

Output

Constant Value 1: Hello
Constant Value 2: Tutorials
Constant Value 3: point

Naming Constants in Swift

Naming a constant is very important. They should have a unique name. You are not allowed to store two constants with the same name if you try to do you will get an error. Swift provides the following rules for naming a constant −

  • Constant names can contain any character including unicode characters. For example, let 你好 = “你好世界".

  • The constant name should not contain whitespace characters, mathematical symbols, arrows, private-se Unicode scalar values, or line and box drawing characters.

  • The name of a constant can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. For example, let myValue = 34.

  • Upper and lowercase letters are distinct because Swift is case-sensitive. For example, let value and let Value are two different constants.

  • Constant names should not begin with a number.

  • You are not allowed to re-declare a constant with the same name. Or cannot change into another type.

  • You are not allowed to change a constant into a variable or vice versa.

  • If you want to declare a constant name the same as a reserved keyword, then use backticks(`) before the name of the constant. For example, let 'var = “hello”.

Printing Swift Constants

You can print the current value of a constant or variable using the print() function. You can interpolate a variable value by wrapping the name in parentheses and escaping it with a backslash before the opening parenthesis.

Example

Swift program to print constant.

import Foundation

// Declaring constants
let constA = "Godzilla"
let constB = 1000.00

// Displaying constant
print("Value of \(constA) is more than \(constB) millions")

Output

Value of Godzilla is more than 1000.0 millions
Advertisements