当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。