Swift Constants

2 min 19 sec read

A swift constant is an entity that doesn’t change whereas a variable is an entity that may change.

For example, when we write

int a = 10
int a = 20
int a = 30

'a' is variable & '10' is constant. Variable 'a' value can be changed to '20' or '30' but you cannot change '10' or '20' or '30' . These are constant values. They are just the numbers.

Constant Declaration in Swift :

To declare constants in Swift 'let' keyword is used. Semicolons are not compulsory in Swift Language. But remember if you add two statements in a single line you have to use ';' as a delimiter to separate them. Otherwise, it shows a syntax error.


let constantName = Constant Value


let name = "Manish"
let a = 20
let b = 20.0

Let's create a sample playground, Goto File > New > Playground > Next.


import UIKit
let a = 20

Run the program in the playground.



Type Annotations in Swift

Annotations are used to specify the type to constant. With the help of Type Annotations, it will be clear what kind of value that specific constant can store. And this can be done by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use.


let constantName:type = value


import UIKit
let name: String
name = "Manish"

let varA:Float = 30.0

Output :-


Print in Swift 3.1

//: Playground - noun: a place where people can play

import UIKit

let name = "Manish"
var age = 23

print("(name) age is = (age)")


Manish age is = 23
Previous Next

Codzify Web Stories

Upskill yourself!

Roadmap to Programming Languages

799 1999 /month

Course Duration: 3 Months.

Mode of Learning: You will get Weekly 3 recorded videos on Monday, Wednesday & Friday.

Live Doubt Sessions: Every Saturday 1 hour, you can opt-for live doubt session if required.

Practical Assignments: Daily practical Assignments to do based on the concepts you learnt in the class.

Things you'll learn: C, C++, Python Programming languages.

Learn More

Roadmap to become skilled Full Stack Web Developer

999 1999

8 hours on-demand video

Full lifetime access

Certificate of completion

Access on mobile and TV

Live Doubt Sessions: Every Saturday 1 hour, you can opt-for live doubt session if required.

Things you'll learn: Angular, HTML, CSS3, Javascript, Bootstrap 5, Server Side Scripting, Google backed Firebase, Authentication, Payment Methods & more.

Learn More