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


Swift Dictionary makeIterator()用法及代碼示例

實例方法

makeIterator()

返回字典鍵值對的迭代器。

聲明

func makeIterator() -> Dictionary<Key, Value>.Iterator
Key 符合 Hashable 時可用。

返回值

具有 (key: Key, value: Value) 類型元素的字典上的迭代器。

詳述

遍曆字典會產生鍵值對作為雙元素元組。您可以在 for - in 循環中分解元組,該循環在後台調用 makeIterator(),或者在直接調用迭代器的 next() 方法時。


let hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]
for (name, hueValue) in hues {
    print("The hue of \(name) is \(hueValue).")
}
// Prints "The hue of Heliotrope is 296."
// Prints "The hue of Coral is 16."
// Prints "The hue of Aquamarine is 156."

可用版本

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

相關用法


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