• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Difference between a constant (let) and variable (var) in Swift

Home cheatsheet Difference between a constant (let) and variable (var) in Swift

Difference between a constant (let) and variable (var) in Swift

Apr 14, 2016 | Posted by Andrew | cheatsheet, ios, swift, tutorial, xcode |

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.

 

Tags: constantletvarvarible
0
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

Swift Lazy Vars Explained

May 6, 2017

What are lazy vars, what do they do. Lazy sounds[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next