Article by: Manish Methani
Published on: October 13, 2021 at 2:04pm
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.
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 print(a)
Run the program in the playground.
20
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" print(name) let varA:Float = 30.0 print(varA)
Manish 30.0
//: 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
Your go-to place to discover the Software Products, latest Tech News and Coding. Ignite Ideas, Unleash Innovation!
Codzify opens the door to a wealth of programming videos, free courses, mock tests, and endless opportunities for learning. Join us today and let your coding journey begin!
Start for Free. Login Now!Explore Coding in Simplified Way with Codzify.com
Online Coding Courses
Simplified Angular 16 Course from Basic to Advanced
Master the Skill of C Porgamming in 30 days.
Flutter App Development Course in Hindi
The Complete 2023 Web Development Bootcamp by Manish Methani | Udemy Course
Complete Flutter App Development Course for Beginners (2023) | Udemy Course
Quick Links