Swift’s zip() Function
Swift provides a solution with a built-in function called zip. This function allows for combining two sequences into a sequence of tuples, enhancing code cleanliness and readability.
1
2
3
4
5
6
7
8
9
import Foundation
let names = ["John", "Harry", "Brook"]
let scores = [72, 92, 78]
let pairedData = zip(names, scores)
for (name, score) in pairedData {
print("\(name) scored \(score) points")
}
Output:
1
2
3
John scored 72 points
Harry scored 92 points
Brook scored 78 points