Explain it simply
Variables & Constants, in plain language
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.
Problem → Why → Solution
The problem — why this concept matters
If you do let userName = "Aung" and then try to reassign it with userName = "Sai", you'll get a compile error.
A common mistake
Assuming one sample output proves nil, ownership, actor isolation, and every error path.
Core concept — understand how it works
Treat Variables & Constants as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
- typed value
- Swift expression or function
- safe result
Explain the type, ownership, and runtime behavior of Variables & Constants
Core syntax or pattern
let userName = "Aung Aung" // a value assigned once and not changed
var loginCount = 1 // a value that can change later
loginCount += 1
print(userName)
print(loginCount)Treat Variables & Constants as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
Read the code step by step
- typed value
let userName = "Aung Aung" // a value assigned once...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.
- Swift expression or function
loginCount += 1Treat Variables & Constants as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
- safe result
print(loginCount)Run and test an original Variables & Constants example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- typed value
- Swift expression or function
- safe result
Success
Explain the type, ownership, and runtime behavior of Variables & Constants
Failure
If you do let userName = "Aung" and then try to reassign it with userName = "Sai", you'll get a compile error.
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.
let userName = "Aung Aung" // a value assigned once and not changed
var loginCount = 1 // a value that can change later
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.
Aung Aung 2Real-world usage
How to apply it in a real project
Run and test an original Variables & Constants example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Mini challenge
Build it yourself
Run and test an original Variables & Constants example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
let userName = "Aung Aung" // a value assigned once and not changed
var loginCount = 1 // a value that can change later
loginCount += 1
print(userName)
print(loginCount)Expected: Explain the type, ownership, and runtime behavior of Variables & Constants
Show hint
Run and test an original Variables & Constants example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- typed value → Swift expression or function → safe result
- Treat Variables & Constants as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
- If you do let userName = "Aung" and then try to reassign it with userName = "Sai", you'll get a compile error.
- Run and test an original Variables & Constants example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
The Swift Programming Language — Swift.org