Swift Arrays - Loops, map, and filter
In this article, we will explain how to loop through a Swift Array, and how to use the filter and map methods.
Looping Through an Array
There are several ways to loop through a Swift Array.
The most basic way is to use a for-in loop.
let names = ["Smith", "Williams", "Miller", "Brown"]
for name in names {
print(name)
}
The output will be as follows, printing each element in order.
Smith
Williams
Miller
Brown
You can also use the forEach() method to print the names in the same way.
let names = ["Smith", "Williams", "Miller", "Brown"]
names.forEach { name in
print(name)
}
The output will be the same as before, printing each element in order.
Smith
Williams
Miller
Brown
The difference from a for-in loop is that break and continue cannot be used in forEach().
If you need to break, continue, or return in the middle of the loop based on a condition, use a for-in loop.
If you want to also get the index of each element while looping, you can use enumerated() like this:
let names = ["Smith", "Williams", "Miller", "Brown"]
for (index, name) in names.enumerated() {
print("\(index): \(name)")
}
The output will include both the index and the element.
0: Smith
1: Williams
2: Miller
3: Brown
Transforming Elements with map()
Using the map() method on a Swift Array, you can generate a new array with transformed elements.
For example, if you want to create a new array with all names converted to uppercase, you can do this:
let names = ["Smith", "Williams", "Miller", "Brown"]
let names_upper = names.map { $0.uppercased() }
print(names)
print(names_upper)
The output will be as follows, where the elements of names_upper are all uppercase.
["Smith", "Williams", "Miller", "Brown"]
["SMITH", "WILLIAMS", "MILLER", "BROWN"]
You can also use map() to extract specific properties from an array of struct or class objects.
For example, if you have an array of Student objects, you can use map() to generate a new array containing only the name property:
struct Student {
var name: String
var age: Int
}
let students = [
Student(name: "Smith", age: 10),
Student(name: "Williams", age: 9),
Student(name: "Miller", age: 8),
Student(name: "Brown", age: 10),
Student(name: "Smith", age: 11)
]
let studentsNames = students.map({ (student) -> String in
return student.name
})
print(studentsNames)
The output will be as follows, showing an array with only the names.
["Smith", "Williams", "Miller", "Brown", "Smith"]
As you can see, map() is useful when you want to transform the elements of an array and create a new one.
Filtering Elements with filter()
Using the filter() method in Swift, you can generate a new array containing only the elements that meet a specified condition.
For example, if you want to create a new array containing only the names that have the letter "l" from the names array, you can do this:
let names = ["Smith", "Williams", "Miller", "Brown"]
let l_names = names.filter { (name) in
return name.contains("l")
}
print(names)
print(l_names)
The output will be as follows, where l_names contains only the names that have the letter "l".
["Smith", "Williams", "Miller", "Brown"]
["Williams", "Miller"]
That's how to loop through a Swift array and use the filter and map methods.