Chapter 69: Swift Mutability (let vs var)

1. The Core Idea – What “mutability” really means

In Swift, mutability = “can this thing be changed after I create it?”

Swift gives you two keywords to control mutability:

  • let = constant → the value cannot be changed after creation (immutable = “I promise this will never change”)
  • var = variable → the value can be changed later (mutable = “this is allowed to change”)

Very important mindset shift (this is what separates beginners from good Swift developers):

In modern Swift, the default recommendation is: Use let by default — everywhere. Use var only when you are sure the value will actually need to change.

This is not just a style rule — it is one of the strongest safety features of Swift.

2. The simplest examples – feel the difference

Swift
Swift

3. The most important rule used by experienced developers

Ask yourself this question every single time you declare something:

“Will this value ever need to be completely replaced with a different value later in this scope?”

  • If the answer is “No” or “Probably not” → use let
  • If the answer is “Yes, definitely” → use var

This single question catches ~80–90% of cases correctly.

4. Real-life examples – how good developers actually choose let vs var

Example 1 – User profile (very common)

Swift

Identity fields → let → Editable fields → var

Example 2 – Settings screen

Swift

→ Fixed constants → let → User preferences → var

Example 3 – Game state (very typical)

Swift

→ Identity & creation data → let → State that changes during gameplay → var

Example 4 – Function parameters & local variables

Swift

Golden habit: Inside functions, default to let for every local variable unless you actually reassign it.

5. Very Common Beginner Mistakes & Correct Habits

Mistake Wrong / Risky code Correct / Better habit Why?
Using var everywhere “just in case” var name = “Priya”; var age = 24; Default to let unless you really reassign let prevents accidental changes + better safety
Reassigning constants by mistake let maxUsers = 100; maxUsers = 200 Compiler error — good! let protects you from bugs
Using var for values that never change var pi = 3.14159 let pi = 3.14159 False expectation — looks like it may change
Declaring loop counters as let for let i in 0..<10 { i += 1 } for i in 0..<10 { … } (implicitly mutable) Loop variable is mutable by default
Making struct properties let when they should be editable struct User { let name: String } var name if user can change it Immutability is good, but not when it blocks real use

6. Quick Summary – Decision Checklist (use this every time)

When you write:

Swift

Ask yourself:

  1. Will I ever need to completely replace this value with a different value later? → Yes → var → No / probably not → let
  2. Is this value part of the identity or fixed state of the object? → Usually let (id, createdAt, email…)
  3. Is this value user-editable or changes over time? → Usually var (displayName, score, isPremium…)
  4. Inside a function — is this a temporary calculation that won’t be reassigned? → Almost always let

7. Small Practice – Choose let or var

For each line, decide let or var and explain why:

Swift

Expected answers (and reasoning):

Swift

Paste your decisions here if you want feedback!

What would you like to explore next?

  • Mutability inside structs vs classes (very important difference)
  • let with computed properties & lazy properties
  • When var is actually safer (mutable state in view models…)
  • Mutability in SwiftUI (@State vs let, @Binding)
  • Or move to another topic (loops, functions, optionals, switch…)

Just tell me — we’ll continue in the same clear, detailed, teacher-like style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *