📖 Scenario: You are managing a simple contact list app where you store names of friends. You want to understand how using let and var affects your ability to change the list.
🎯 Goal: Build a Swift program that creates a collection of friend names, sets a mutability flag, modifies the collection accordingly, and finalizes the collection to see how let and var control mutability.
📋 What You'll Learn
Create an array of friend names using var or let as instructed
Add a Boolean variable to indicate if the collection is mutable
Use an if statement to add a new friend only if the collection is mutable
Finalize the collection declaration to reflect the mutability
💡 Why This Matters
🌍 Real World
Managing lists of items like contacts, tasks, or products where you need to control if the list can be changed or not.
💼 Career
Understanding mutability is crucial for writing safe and predictable Swift code, especially in app development where data integrity matters.
Progress0 / 4 steps
1
Create a mutable array of friend names
Create a variable called friends using var and assign it an array with these exact names: "Alice", "Bob", "Charlie".
Swift
Hint
Use var friends = ["Alice", "Bob", "Charlie"] to create a mutable array.
2
Add a mutability flag
Create a constant Boolean variable called isMutable and set it to true.
Swift
Hint
Use let isMutable = true to create a constant Boolean.
3
Add a new friend if mutable
Use an if statement to check if isMutable is true. Inside the block, append the name "Diana" to the friends array.
Swift
Hint
Use if isMutable { friends.append("Diana") } to add a friend only if mutable.
4
Change collection to immutable and finalize
Change the declaration of friends from var to let to make the array immutable. Keep the rest of the code the same.
Swift
Hint
Change var to let to make the array immutable.
Practice
(1/5)
1. In Swift, if you declare a collection with let, what does it mean for that collection?
easy
A. The collection cannot be changed after creation.
B. You can add or remove items anytime.
C. Only the items inside can change, but not the collection size.
D. The collection is deleted automatically.
Solution
Step 1: Understand let in Swift
let creates a constant, meaning the value cannot be changed after it is set.
Step 2: Apply to collections
If a collection is declared with let, you cannot add, remove, or modify its elements because the collection itself is constant.
Final Answer:
The collection cannot be changed after creation. -> Option A
Quick Check:
let means constant collection [OK]
Hint: Remember: let means no changes allowed to collection [OK]
Common Mistakes:
Thinking you can add items to a let collection
Confusing let with var for collections
Assuming only items can change but not size
2. Which of the following is the correct way to declare a mutable array in Swift?
easy
A. mutable numbers = [1, 2, 3]
B. let numbers = [1, 2, 3]
C. const numbers = [1, 2, 3]
D. var numbers = [1, 2, 3]
Solution
Step 1: Recall Swift syntax for mutable collections
Use var to declare variables that can change.
Step 2: Check options
var numbers = [1, 2, 3] correctly declares a mutable array. let is immutable, and const or mutable are not Swift keywords.
Final Answer:
var numbers = [1, 2, 3] -> Option D
Quick Check:
Mutable array uses var [OK]
Hint: Use var for collections you want to change [OK]
Common Mistakes:
Using let instead of var for mutable collections
Using non-Swift keywords like const or mutable
Confusing var with let
3. What will be the output of this Swift code?
let fruits = ["Apple", "Banana"]
fruits.append("Cherry")
print(fruits)
medium
A. Compile-time error
B. ["Apple", "Banana"]
C. ["Apple", "Banana", "Cherry"]
D. Runtime crash
Solution
Step 1: Identify collection mutability
The array fruits is declared with let, making it immutable.
Step 2: Analyze append operation
Trying to append to an immutable array causes a compile-time error because you cannot modify a constant collection.
Final Answer:
Compile-time error -> Option A
Quick Check:
Append on let collection = Compile-time error [OK]
Hint: Appending to let collection causes compile error [OK]
Common Mistakes:
Assuming append works on let collections
Thinking it crashes at runtime instead of compile-time
Expecting original array unchanged
4. Consider this Swift code snippet:
let numbers = [1, 2, 3]
numbers[0] = 10
print(numbers)
What is the problem here?
medium
A. The array must be declared with var to print.
B. You cannot change elements of an array declared with let.
C. The print statement is incorrect.
D. Array index 0 does not exist.
Solution
Step 1: Check array declaration
The array numbers is declared with let, so it is immutable.
Step 2: Understand element assignment
Trying to assign a new value to an element of an immutable array is not allowed and causes a compile-time error.
Final Answer:
You cannot change elements of an array declared with let. -> Option B
Quick Check:
Changing element in let array = Not allowed [OK]
Hint: Elements of let arrays cannot be changed [OK]
Common Mistakes:
Thinking only size is fixed but elements can change
Assuming index 0 is invalid
Believing print causes error
5. You want to create a dictionary in Swift that you can update later by adding or changing keys and values. Which declaration should you use and why?
hard
A. Use let and then create a new dictionary to update.
B. Use let because it allows changing keys and values.
C. Use var because it allows modifying the dictionary after creation.
D. Use var but you cannot change keys or values.
Solution
Step 1: Understand mutability of dictionaries
To modify a dictionary after creation, it must be declared mutable.
Step 2: Choose correct keyword
var declares a mutable dictionary allowing adding or changing keys and values. let makes it immutable.
Final Answer:
Use var because it allows modifying the dictionary after creation. -> Option C
Quick Check:
Mutable dictionary needs var [OK]
Hint: Use var for collections you want to update later [OK]
Common Mistakes:
Using let and expecting to modify dictionary
Thinking let allows changing keys or values
Believing var prevents changing keys or values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.