Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
In Swift, arrays and dictionaries are two commonly used collection types. You can use loops to iterate over the elements of these collections. Here's how you can do it:
Arrays in Swift are ordered collections of elements.
let fruits = ["Apple", "Banana", "Cherry"] // Using a for-in loop for fruit in fruits { print(fruit) } // If you also want the index of each item for (index, fruit) in fruits.enumerated() { print("Item \(index + 1): \(fruit)") }
Dictionaries in Swift are collections of key-value pairs. The order of elements in a dictionary is not guaranteed until Swift 5. When you iterate over a dictionary, you'll get a tuple containing the key and value of each item.
let capitals = ["USA": "Washington, D.C.", "France": "Paris", "Japan": "Tokyo"] // Using a for-in loop for (country, capital) in capitals { print("The capital of \(country) is \(capital)") }
Note: As of Swift 5, dictionaries maintain the order of their elements in the order they were added. Before Swift 5, dictionaries were unordered.
You can also combine loops with conditional statements and the where
keyword to iterate over items that meet certain criteria:
let numbers = [10, 15, 20, 25, 30] // Print only even numbers for number in numbers where number % 2 == 0 { print(number) }
Remember, Swift provides a range of powerful tools, including higher-order functions like map
, filter
, and reduce
, which can often be used as alternatives to loops for iterating and transforming collections. However, the basic for-in
loop is a straightforward and commonly used method for iteration.
Looping through arrays in Swift is a common operation to iterate over each element. You can use various constructs like for-in
loops for this purpose.
Here's a basic example of iterating over elements in an array:
let numbers = [1, 2, 3, 4, 5] for number in numbers { print(number) }
Output:
1 2 3 4 5
The for-in
loop simplifies the process of iterating through arrays:
let fruits = ["Apple", "Banana", "Orange"] for fruit in fruits { print(fruit) }
Output:
Apple Banana Orange
Swift provides the enumerated()
method for arrays, which returns a sequence of pairs (index, element). This is useful when you need both the index and the element:
let colors = ["Red", "Green", "Blue"] for (index, color) in colors.enumerated() { print("Index: \(index), Color: \(color)") }
Output:
Index: 0, Color: Red Index: 1, Color: Green Index: 2, Color: Blue
Looping through dictionaries involves iterating over key-value pairs.
Here's an example of looping through dictionary key-value pairs:
let person = ["name": "John", "age": 25, "city": "New York"] for (key, value) in person { print("\(key): \(value)") }
Output:
name: John age: 25 city: New York
You can use the for-in
loop directly with dictionaries:
let grades = ["Math": 90, "English": 85, "History": 88] for (subject, score) in grades { print("Subject: \(subject), Score: \(score)") }
Output:
Subject: Math, Score: 90 Subject: English, Score: 85 Subject: History, Score: 88
You can combine enumeration with loops to get both index and value:
let animals = ["Cat", "Dog", "Elephant"] for (index, animal) in animals.enumerated() { print("Index: \(index), Animal: \(animal)") }
Output:
Index: 0, Animal: Cat Index: 1, Animal: Dog Index: 2, Animal: Elephant