當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Swift Zip2Sequence用法及代碼示例

結構

Zip2Sequence

由兩個底層序列構成的對序列。

聲明

@frozen struct Zip2Sequence<Sequence1, Sequence2> where Sequence1 : Sequence, Sequence2 : Sequence

概述

Zip2Sequence 實例中,第i 對的元素是每個基礎序列的第i 元素。要創建 Zip2Sequence 實例,請使用 zip(_:_:) 函數。

以下示例使用 zip(_:_:) 函數同時迭代字符串數組和可數範圍:


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"

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

相關用法


注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Zip2Sequence。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。