Swift Constants

In swift, Constants are used to define fixed values throughout the application and it will throw an error in case if we are trying to modify the value of constant in the application. We can store different types of data in constants like integers, strings, characters, bool, etc.

 

Whenever we create a constant in the swift programming language, automatically the required space is allocated in computer memory for that constant, and the space allocation in memory is varied based on the data type of constant like integer or float or double, etc.

 

Constants are same as Variables but the only difference is constant values cannot be modified once it defined in the application.

Swift Constants Declaration

In swift, we need to define constants before they used it in our application. By using the “let” keyword we can declare constants in a swift programming language.

 

Following is the syntax of declaring constant in a swift programming language.

 

let <nameofConstant> = <Initial Value>

In the above syntax, we used the “let” keyword to declare the constants. Now we implement simple examples like as shown below in Xcode editor using constants.

 

let str = "Welcome to Tutlane"

print(str)

In the above code, the first line tells the compiler that we declared a constant with the name “str” and assign its value, by doing this compiler will allocate the required space in our computer memory to hold the constant value and in the second-line, we will print “str” constant by its name.

 

Following is the result of the above swift constant declaration program.

 

Welcome to Tutlane

Swift Type Annotations

In swift by defining type annotation while declaring a constant, we can easily know what kind of values the constant can store. 

 

We can define constant type annotation by placing a colon after the constant name, followed by a space and followed by the name of the type to use.

 

Following is the syntax of defining type annotation to the constant in Swift programming language.

 

let conName: <data type> = <initial value>

Following is the simple example of defining type annotation to the constant in our Xcode Editor.

 

let str: String = "Welcome to Tutlane"

print(str)

If you observe the above example we defined constant “str” with its data type (String). The above swift program will return value like as shown below.

 

Welcome to Tutlane

Note: when we declare a constant with type annotation then it’s mandatory to mention the initial value of constant corresponds to its data type.

 

Following is another example of defining constant with its data-type

 

let number: Float = 2334.5664

print(number)

The above swift program will return the result as shown below.

 

2334.5664

Swift Naming Constants

In swift, constant names can contain almost all the characters like strings, numbers, underscore, and including Unicode characters but it should not contain any whitespace characters, mathematical symbols, arrows, and invalid Unicode code points. 

 

The constant name should not start with numbers but it can contain the numbers elsewhere in the name. Following is a simple example of naming constants in the swift programming language.

 

let val123 = "Welcome"

print(val123)

 

let _sampleVal = "Tutlane"

print(_sampleVal)

 

let 你好= "你好世界"

print(你好)

If you observe the above example we used simple Unicode characters also as constant names. For the above example, the swift playground will return results as shown below.

 

Welcome

Tutlane

你好世界

Swift Constants Concatenation & Interpolation

In swift we can combine two constants into a single constant for that we have two ways, one is we can concatenate it and second is to interpolate constants with parenthesis.

 

Following is the simple example of concatenating two constants into a single constant using swift.

 

let var1 = "Welcome to "

let var2 = "Tutlane"

 

let result = var1 + var2

print(result)

The swift programming playground will return the result as shown below.

 

Welcome to Tutlane

Now we will see how to use interpolation to combine two constants with examples. Following is a simple example of using interpolation to combine two constants in swift.

 

let var1 = "Welcome to "

let var2 = "Tutlane"

print("/(var1) /(var2)")

The above swift program will return results like as shown below.

 

Welcome to Tutlane

Swift Print Constants

In swift, we can print constants using a function called “print”. Following is a simple example of printing constants.

 

let var1 = "Welcome to "

let var2 = "Tutlane"

 

let result = var1 + var2

print(result)

For the above program, the swift programming playground will return the result as shown below.

 

Welcome to Tutlane

This is how we can define, declare and use constants in a swift programming language based on our requirements.