Variables store information you want to use later. Using let and var helps you keep your data safe and clear.
Variables (let, var) and type inference in iOS Swift
Introduction
Syntax
iOS Swift
let constantName: Type = value var variableName: Type = value // Or with type inference: let constantName = value var variableName = value
let creates a constant that cannot change after it is set.
var creates a variable that can change its value later.
Examples
pi with type Double that cannot change.iOS Swift
let pi: Double = 3.14159score with type Int that can change.iOS Swift
var score: Int = 0name is a String because of the value.iOS Swift
let name = "Alice"isLoggedIn is a Bool because of the value.iOS Swift
var isLoggedIn = false
Sample App
This SwiftUI app shows a score that starts at 0 and can increase by pressing a button. The max score is a constant that never changes.
iOS Swift
import SwiftUI struct ContentView: View { @State private var score = 0 let maxScore = 10 var body: some View { VStack(spacing: 20) { Text("Score: \(score)") .font(.largeTitle) Button("Increase Score") { if score < maxScore { score += 1 } } Text("Max score is fixed at \(maxScore)") .foregroundColor(.gray) } .padding() } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
Important Notes
Use let whenever you know the value won't change to avoid bugs.
Swift's type inference helps you write less code but you can always specify types if you want.
Changing a let constant after setting it will cause an error.
Summary
let creates constants that cannot change.
var creates variables that can change.
Swift can guess the type of a variable or constant from its value.
Practice
1. What does the keyword
let do in Swift?easy
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]
Hint: Remember: let = fixed value, var = changeable value [OK]
Common Mistakes:
- Confusing let with var
- Thinking let allows value changes
- Mixing let with function declaration
2. Which of the following is the correct way to declare a variable named
score with initial value 10 in Swift?easy
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]
Hint: Use var for variables, let for constants [OK]
Common Mistakes:
- Using let instead of var for variables
- Incorrect keyword order
- Using non-existent keywords like variable
3. What will be the output of this Swift code?
var name = "Alice" name = "Bob" print(name)
medium
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]
Hint: var means value can change, so print latest value [OK]
Common Mistakes:
- Thinking let was used and reassignment causes error
- Expecting original value to print
- Confusing variable with constant
4. What is wrong with this Swift code?
let age = 25 age = 30
medium
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]
Hint: let means constant; reassignment causes error [OK]
Common Mistakes:
- Thinking semicolon is required
- Confusing let with var
- Ignoring reassignment rules
5. Consider this Swift code:
What will be printed and why?
var x = 5 let y = x x = 10 print(y)
What will be printed and why?
hard
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]
Hint: let copies value at assignment; later var changes don't affect it [OK]
Common Mistakes:
- Thinking y updates when x changes
- Expecting runtime error on assignment
- Confusing reference and value types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
