📖 Scenario: You are managing a simple shopping list app. You need to keep track of items you want to buy.
🎯 Goal: Build a Swift program that creates a shopping list array, adds items, inserts an item at a specific position, removes an item, and then shows the final list.
📋 What You'll Learn
Create an array called shoppingList with initial items
Add a new item to the end of shoppingList using append
Insert an item at a specific position using insert
Remove an item by its value using removeAll(where:)
Print the final shoppingList array
💡 Why This Matters
🌍 Real World
Managing lists like shopping lists, to-do lists, or any collection of items is common in apps and daily tasks.
💼 Career
Understanding how to manipulate arrays is essential for many programming jobs, especially in app development and data handling.
Progress0 / 4 steps
1
Create the initial shopping list array
Create an array called shoppingList with these exact items: "Milk", "Eggs", and "Bread".
Swift
Hint
Use var shoppingList = ["Milk", "Eggs", "Bread"] to create the array.
2
Add a new item to the shopping list
Add the item "Butter" to the end of the shoppingList array using the append method.
Swift
Hint
Use shoppingList.append("Butter") to add the item.
3
Insert an item at a specific position
Insert the item "Cheese" at index 1 in the shoppingList array using the insert method.
Swift
Hint
Use shoppingList.insert("Cheese", at: 1) to insert the item.
4
Remove an item and print the final list
Remove the item "Eggs" from the shoppingList array using removeAll(where:). Then print the shoppingList array.
Swift
Hint
Use shoppingList.removeAll(where: { $0 == "Eggs" }) to remove the item, then print(shoppingList) to show the list.
Practice
(1/5)
1. What does the append method do in a Swift array?
easy
A. Removes the last element from the array
B. Adds a new element to the end of the array
C. Inserts an element at the beginning of the array
D. Clears all elements from the array
Solution
Step 1: Understand the append method
The append method adds a new element to the end of the array, increasing its size by one.
Step 2: Compare with other options
Removing elements or inserting at the beginning are done by other methods, not append.
Final Answer:
Adds a new element to the end of the array -> Option B
Quick Check:
append = add at end [OK]
Hint: append always adds at the end of the array [OK]
Common Mistakes:
Confusing append with insert
Thinking append removes elements
Assuming append adds at the start
2. Which of the following is the correct way to insert the number 10 at index 2 in a Swift array named numbers?
easy
A. numbers.insert(10, at: 2)
B. numbers.append(10, at: 2)
C. numbers.add(10, index: 2)
D. numbers.insertAt(2, 10)
Solution
Step 1: Recall the insert syntax in Swift
The correct syntax is array.insert(element, at: index), so numbers.insert(10, at: 2) is correct.
Step 2: Check other options for syntax errors
Options A, B, and D use incorrect method names or argument orders.
Final Answer:
numbers.insert(10, at: 2) -> Option A
Quick Check:
insert(element, at: index) syntax [OK]
Hint: Use insert(element, at: index) to add anywhere [OK]
Common Mistakes:
Using append with index
Wrong method names like add or insertAt
Swapping argument order
3. What will be the output of the following Swift code?
var fruits = ["Apple", "Banana", "Cherry"]
fruits.append("Date")
fruits.remove(at: 1)
print(fruits)
medium
A. ["Banana", "Cherry", "Date"]
B. ["Apple", "Banana", "Cherry", "Date"]
C. ["Apple", "Cherry", "Date"]
D. ["Apple", "Date"]
Solution
Step 1: Append "Date" to the fruits array
After append, fruits = ["Apple", "Banana", "Cherry", "Date"].
Step 2: Remove element at index 1
Index 1 is "Banana", so removing it results in ["Apple", "Cherry", "Date"].