Swift Constants

  Total Views : 311
  Comments : 0
  Keywords : Swift constants cannot be modified once it defined in application with examples, Constants in swift defines fixed values throughout the project with examples

Here we will learn constants in swift with examples and how to use constants in swift, constants declaration in swift, constants type annotations in swift, naming constants and concatenating constants in swift with examples.

Swift Constants

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

 

Whenever we create a constant in 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 only difference is constant values cannot be modified once it defined in application.

Swift Constants Declaration

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

 

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

 

let <nameofConstant> = <Initial Value>

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

 

let str = "Welcome to Tutlane"

print(strr)

In above code first line tells the compiler that we declared a constant with 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 second line we will print “str” constant by its name.

 

Following is the result of 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 that 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(strr)

If you observe 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 initial value of constant correspond to its data type.

 

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

 

let number: Float = 2334.5664

print(number)

The above swift program will return the result like 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 simple example of naming constants in swift programming language.

 

let val123 = "Welcome"

print(val123)

 

let _sampleVal = "Tutlane"

print(_sampleVal)

 

let 你好= "你好世界"

print(你好)

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

 

Welcome

Tutlane

你好世界

Swift Constants Concatenation & Interpolation

In swift we can combine two constants into 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 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 like as shown below.

 

Welcome to Tutlane

Now we will see how to use interpolation to combine two constants with examples. Following is 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 result like as shown below.

 

Welcome to Tutlane

Swift Print Constants

In swift we can print constants using function called “print”. Following is the 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 like as shown below.

 

Welcome to Tutlane

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

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User