What if your program could remember changes just like you do, without extra effort?
Why Var for variables (mutable) in Swift? - Purpose & Use Cases
Imagine you are writing down your daily expenses on paper. Every time you spend money, you have to erase the old total and write the new one. This takes time and can cause mistakes if you forget to update the number.
Manually updating values is slow and easy to mess up. If you forget to change a number or write it wrong, your whole calculation becomes wrong. It's hard to keep track of changes and fix errors.
Using var in Swift lets you create variables that can change their value easily. You just update the variable, and the program remembers the new value for you. This makes your code flexible and less error-prone.
let total = 10 // To change total, you must create a new constant or do manual recalculation
var total = 10 total = 15 // Just update the variable directly
It allows your program to remember and update information as things change, just like you do in real life.
Tracking your bank account balance where deposits and withdrawals change the total amount over time.
Variables let you store values that can change.
Using var in Swift makes updating values easy and safe.
This helps your programs handle real-world changing data smoothly.
Practice
var keyword do in Swift?Solution
Step 1: Understand the purpose of
Thevarvarkeyword is used to declare variables that can be changed after they are set.Step 2: Compare with constants
Unlikelet, which creates constants,varallows the value to be updated.Final Answer:
It creates a variable whose value can change later. -> Option AQuick Check:
varmeans mutable variable [OK]
- Confusing var with let (constant)
- Thinking var creates constants
- Mixing var with function declaration
score with initial value 10 in Swift?Solution
Step 1: Identify the keyword for mutable variables
In Swift,vardeclares a variable that can change, so usevar score = 10.Step 2: Check other options
letcreates a constant,variableandconstare not valid Swift keywords.Final Answer:
var score = 10 -> Option DQuick Check:
Mutable variable usesvar[OK]
- Using let instead of var for mutable variables
- Using invalid keywords like variable or const
- Missing the equals sign
var count = 5 count = 10 print(count)
Solution
Step 1: Understand variable assignment
The variablecountis declared withvarand initially set to 5, so it can be changed.Step 2: Update and print value
The value is changed to 10 before printing, soprint(count)outputs 10.Final Answer:
10 -> Option AQuick Check:
Mutable var updates value correctly [OK]
- Thinking var acts like let (constant)
- Expecting original value instead of updated
- Confusing syntax causing errors
var name = "Alice" name = 42 print(name)
Solution
Step 1: Check variable type inference
Variablenameis inferred as String because of initial value "Alice".Step 2: Assign incompatible type
Assigning an integer 42 to a String variable causes a type mismatch error.Final Answer:
Type mismatch error when assigning 42 to name -> Option CQuick Check:
Swift variables keep type, cannot assign different type [OK]
- Ignoring type mismatch errors
- Thinking var allows any type changes
- Confusing syntax error with type error
var?
1. let score = 0 2. score = 10 3. var score = 0 4. score = 10 5. var score: String 6. score = 10 7. let score: Int 8. score = 10
Solution
Step 1: Identify mutable variable declaration
Lines 3 and 4 declarescorewithvarand assign 0, then update to 10, which is correct.Step 2: Check other options
Lines 1 and 2 uselet(constant), so line 2 causes error. Lines 5 and 6 declarescoreasStringbut assign integer 10, causing type mismatch error. Lines 7 and 8 useletconstant and then try to assign, causing error.Final Answer:
Lines 3 and 4 -> Option BQuick Check:
Use var to declare and update variables [OK]
- Using let for variables that change
- Type mismatch with wrong type annotation
- Confusing var and let usage
Ready to prove it?
Pass the test and this pattern moves unknown
