0
0
Swiftprogramming~15 mins

Array iteration and enumerated in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Array iteration and enumerated
📖 Scenario: You are helping a small bookstore keep track of book titles and their positions on a shelf.
🎯 Goal: You will create an array of book titles, then use enumerated() to print each book's position and title.
📋 What You'll Learn
Create an array called books with exactly these titles: "Swift Basics", "iOS Development", "Data Structures"
Create a variable called startIndex and set it to 1
Use a for loop with enumerated() on books to get index and title
Print the position starting from startIndex and the book title in the format: "Book 1: Swift Basics"
💡 Why This Matters
🌍 Real World
Bookstores or libraries often need to list items with their positions or numbers for easy reference.
💼 Career
Knowing how to loop through arrays with indexes is important for many programming tasks like displaying lists, processing data, or creating menus.
Progress0 / 4 steps
1
Create the books array
Create an array called books with these exact titles in order: "Swift Basics", "iOS Development", "Data Structures"
Swift
Need a hint?

Use square brackets [] to create an array and separate titles with commas.

2
Set the start index
Create a variable called startIndex and set it to 1
Swift
Need a hint?

Use let to create a constant variable named startIndex.

3
Loop with enumerated()
Use a for loop with enumerated() on books to get index and title
Swift
Need a hint?

Use for (index, title) in books.enumerated() to get both position and value.

4
Print book positions and titles
Inside the loop, print the position starting from startIndex plus index and the book title in this exact format: "Book 1: Swift Basics"
Swift
Need a hint?

Use print("Book \(startIndex + index): \(title)") inside the loop.