Difference between a constant (let) and variable (var) in Swift
The difference between let and var can be confusing in Swift so let’s clear up the difference.
let is what is known as a constant. Once you set it once you cannot change it, like your Age it life it cannot be changed!
For Example:
let myAge = 24
var also known as variable can be changed once set, like the weather can change all the time.
For Example:
var weather = "Sunny" weather = "Rainy" weather = "Cloudy"
So why don’t we just always use variables then and why does Swift warn us if we have a variable that never changes complaining to us to change it to a let?
Well it is good programming practice to set things that do not change to a let, remember it’s not just the computer reading your code, it is yourself and fellow developers! It also guards against risky errors like chaining a value you shouldn’t be changing.
It is also slightly more efficient code due to compiler optimizations however with computing power these days the difference is negligible.