Thuta Learning
BasicProgrammingbeginner

Variables & Constants

Relax. We'll talk through this in plain words — no textbook voice.

Swift gives you two ways to store data: let and var. let is for a constant whose value won't change, and var is for a variable that can change later.

In real projects, if a value doesn't need to change, it's best to default to let. It makes your code safer and cuts down on bugs caused by values changing when you didn't expect them to.

swift
let userName = "Aung Aung"   // တစ်ကြိမ်သတ်မှတ်ပြီး မပြောင်းတော့မယ့်တန်ဖိုး
var loginCount = 1          // နောက်ပိုင်းပြောင်းနိုင်တဲ့တန်ဖိုး

loginCount += 1

print(userName)
print(loginCount)

userName is declared with let, so its value can't be reassigned. loginCount is a var, so we can bump it up with += 1.

You should see
Aung Aung 2

Easy traps

  • If you do let userName = "Aung" and then try to reassign it with userName = "Sai", you'll get a compile error.
Variables & Constants | Thuta Learning