Generic Function


Creates a sequence of pairs built out of two underlying sequences.


func zip<Sequence1, Sequence2>(_ sequence1: Sequence1, _ sequence2: Sequence2) -> Zip2Sequence<Sequence1, Sequence2> where Sequence1 : Sequence, Sequence2 : Sequence



The first sequence or collection to zip.


The second sequence or collection to zip.

Return Value

A sequence of tuple pairs, where the elements of each pair are corresponding elements of sequence1 and sequence2.


In the Zip2Sequence instance returned by this function, the elements of the ith pair are the ith elements of each underlying sequence. The following example uses the zip(_:_:) function to iterate over an array of strings and a countable range at the same time:

let words = ["one", "two", "three", "four"]
let numbers = 1...4

for (word, number) in zip(words, numbers) {
    print("\(word): \(number)")
// Prints "one: 1"
// Prints "two: 2
// Prints "three: 3"
// Prints "four: 4"

If the two sequences passed to zip(_:_:) are different lengths, the resulting sequence is the same length as the shorter sequence. In this example, the resulting array is the same length as words:

let naturalNumbers = 1...Int.max
let zipped = Array(zip(words, naturalNumbers))
// zipped == [("one", 1), ("two", 2), ("three", 3), ("four", 4)]


Generic Constraints

  • Sequence1 : Sequence
  • Sequence2 : Sequence