What if your app could remember and change information all by itself, without you writing extra code every time?
Why Variables (let, var) and type inference in iOS Swift? - Purpose & Use Cases
Imagine you are building a simple app that tracks a user's score and name. Without variables, you would have to write separate code for every single value and update it everywhere manually.
This manual approach is slow and confusing. You might forget to update a value or mix up data types, causing bugs that are hard to find. It's like trying to remember every detail without writing anything down.
Using variables with let and var lets you store and update information easily. Swift's type inference means you don't always have to say the data type explicitly, making your code cleaner and faster to write.
var score: Int = 0 score = 10 var name: String = "Alice"
var score = 0 score = 10 let name = "Alice"
This lets you write clear, safe, and flexible code that can change as your app runs, without extra hassle.
Think of a game app where the player's score changes constantly, but their username stays the same. Using var for score and let for username makes this easy and error-free.
Variables store data that can change (var) or stay constant (let).
Type inference lets Swift guess the data type, so you write less code.
This makes your app code simpler, safer, and easier to update.
Practice
let do in Swift?Solution
Step 1: Understand
The keywordletusageletis used to declare constants in Swift, which means their value cannot be changed after assignment.Step 2: Compare with
Unlikevarvar, which declares variables that can change,letmakes the value fixed.Final Answer:
Creates a constant that cannot be changed -> Option AQuick Check:
let= constant [OK]
- Confusing let with var
- Thinking let allows value changes
- Mixing let with function declaration
score with initial value 10 in Swift?Solution
Step 1: Identify variable declaration syntax
In Swift,varis used to declare variables that can change, and the syntax isvar name = value.Step 2: Check each option
var score = 10uses correct syntax.let score = 10declares a constant, not a variable.score var = 10andvariable score = 10are invalid syntax.Final Answer:
var score = 10 -> Option BQuick Check:
Variable declaration = var name = value [OK]
- Using let instead of var for variables
- Incorrect keyword order
- Using non-existent keywords like variable
var name = "Alice" name = "Bob" print(name)
Solution
Step 1: Understand variable reassignment
The variablenameis declared withvar, so it can be changed after initial assignment.Step 2: Trace the code execution
Initially,nameis "Alice". Then it is reassigned to "Bob". The print statement outputs the current value, "Bob".Final Answer:
Bob -> Option CQuick Check:
var allows change, so output is reassigned value [OK]
- Thinking let was used and reassignment causes error
- Expecting original value to print
- Confusing variable with constant
let age = 25 age = 30
Solution
Step 1: Identify declaration type
ageis declared withlet, making it a constant.Step 2: Understand reassignment error
Trying to assign a new value to a constant causes a compile-time error: "Cannot assign to value: 'age' is a constant".Final Answer:
Cannot assign to value: 'age' is a constant -> Option AQuick Check:
let constants cannot be changed [OK]
- Thinking semicolon is required
- Confusing let with var
- Ignoring reassignment rules
var x = 5 let y = x x = 10 print(y)
What will be printed and why?
Solution
Step 1: Understand value assignment
Variablexis 5 initially.yis a constant assigned the current value ofx, which is 5.Step 2: Reassign x and check y
Changingxto 10 does not affecty, becauseyholds its own copy of the value 5.Final Answer:
5, because y is a constant copy of x's initial value -> Option DQuick Check:
let copies value; later changes to var don't affect it [OK]
- Thinking y updates when x changes
- Expecting runtime error on assignment
- Confusing reference and value types
Ready to prove it?
Pass the test and this pattern moves unknown
