0
0
Swiftprogramming~15 mins

Array operations (append, insert, remove) in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Array operations (append, insert, remove)
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use shoppingList.removeAll(where: { $0 == "Eggs" }) to remove the item, then print(shoppingList) to show the list.