ahcode0919
3/25/2017 - 5:33 PM

Swift Zip function usage

Swift Zip function usage

//Zip function (Sequence)
// https://github.com/apple/swift/blob/master/stdlib/public/core/Zip.swift
// https://developer.apple.com/reference/swift/1541125-zip
// http://swiftdoc.org/v3.0/func/zip/

let array1 = ["one", "two", "three", "four", "five"]
let array2 = 1...5

let zipSequence = zip(array1, array2)
type(of: zipSequence)   //Zip2Sequence<Array<String>, CountableClosedRange<Int>>.Type

zipSequence.forEach { (key, value) in
    print("key: \(key), value: \(value)")
}

//key: one, value: 1
//key: two, value: 2
//key: three, value: 3
//key: four, value: 4
//key: five, value: 5