为什么 Dictionary.map return 是一个元组数组,它​​在哪里记录?

Why does Dictionary.map return an array of tuples, and where is it documented?

考虑以下代码:

let dict = [
  "key1" : 1,
  "key2" : 2,
  "key3" : 3,
  "key4" : 4,
  "key5" : 5
]

let array = dict.map{[=12=]}
for item in array {
  print(item)
}

您从打印语句中得到的是:

("key2", 2)
("key3", 3)
("key4", 4)
("key5", 5)
("key1", 1)

字典中的 key/value 对被转换为元组。我本以为会得到一组单值字典。

为什么 map 语句将我的项目转换为元组,这种行为记录在何处?

使用以下代码将元组数组转换回字典数组是一件简单的事情:

let array = dict.map{[[=14=].0:[=14=].1]}

...但我试图理解为什么 map 首先给我元组。

它是 DictionaryIterator<Key, Value> 的一部分。请参阅 HashedCollections 模块中关于 makeIterator 的注释:

/// Returns an iterator over the dictionary's key-value pairs.
///
/// Iterating over a dictionary yields the key-value pairs as two-element
/// tuples. You can decompose the tuple in a `for`-`in` loop, which calls
/// `makeIterator()` behind the scenes, or when calling the iterator's
/// `next()` method directly.
///
///     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."
///
/// - Returns: An iterator over the dictionary with elements of type
///   `(key: Key, value: Value)`.
public func makeIterator() -> DictionaryIterator<Key, Value>