Swift Tips: Zip different sized arrays

Tuğçe Arar
Nov 13, 2022
wonderland by ibenzani

Swift uses the first option, the resulting sequence will have a length equal to the shorter of the two inputs.

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

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

Examples:

let words = ["one", "two", "three", "four"]
let numbers = [1, 2, 3]
let zippedArray = zip(words, numbers)
print(/("Count:"zippedArray))
//prints Count:3
//zippedArray --> [("one",1),("two",2),("three",3)]

--

--