Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 为什么Dictionary.map返回元组数组,它在哪里记录?_Swift_Dictionary_Map Function - Fatal编程技术网

Swift 为什么Dictionary.map返回元组数组,它在哪里记录?

Swift 为什么Dictionary.map返回元组数组,它在哪里记录?,swift,dictionary,map-function,Swift,Dictionary,Map Function,考虑以下代码: let dict = [ "key1" : 1, "key2" : 2, "key3" : 3, "key4" : 4, "key5" : 5 ] let array = dict.map{$0} for item in array { print(item) } 您从打印报表中得到的是: ("key2", 2) ("key3", 3) ("key4", 4) ("key5", 5) ("key1", 1) 字典中的键/值对转换为元组。我本来希望能得

考虑以下代码:

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

let array = dict.map{$0}
for item in array {
  print(item)
}
您从打印报表中得到的是:

("key2", 2)
("key3", 3)
("key4", 4)
("key5", 5)
("key1", 1)
字典中的键/值对转换为元组。我本来希望能得到一个单值字典数组

为什么map语句将我的项转换为元组,这种行为记录在哪里

使用以下代码将元组数组转换回字典数组很简单:

let array = dict.map{[$0.0:$0.1]}

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

它是
字典迭代器的一部分。有关
makeIterator
,请参见
HashedCollections
模块中的注释:

/// 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>
///返回字典键值对上的迭代器。
///
///在字典上迭代会产生两个元素的键值对
///元组。您可以将元组分解为`for`-`in`循环,该循环调用
///`makeIterator()`在幕后,或在调用迭代器的
///直接使用'next()'方法。
///
///让色调=[“Heliotrope”:296,“珊瑚”:16,“海蓝宝石”:156]
///用色调表示(名字,hueValue){
///印刷品(“名称的色调为色调”)
///     }
/////打印“Heliotrope的色调为296。”
/////打印“珊瑚的色调是16。”
/////打印“海蓝宝石的色调是156。”
///
///-返回:字典上的迭代器,元素类型为
///`(键:键,值:值)`。
public func makeIterator()->DictionaryIterator

它是
字典迭代器的一部分。有关
makeIterator
,请参见
HashedCollections
模块中的注释:

/// 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>
///返回字典键值对上的迭代器。
///
///在字典上迭代会产生两个元素的键值对
///元组。您可以将元组分解为`for`-`in`循环,该循环调用
///`makeIterator()`在幕后,或在调用迭代器的
///直接使用'next()'方法。
///
///让色调=[“Heliotrope”:296,“珊瑚”:16,“海蓝宝石”:156]
///用色调表示(名字,hueValue){
///印刷品(“名称的色调为色调”)
///     }
/////打印“Heliotrope的色调为296。”
/////打印“珊瑚的色调是16。”
/////打印“海蓝宝石的色调是156。”
///
///-返回:字典上的迭代器,元素类型为
///`(键:键,值:值)`。
public func makeIterator()->DictionaryIterator

ooh,我学到了一些我不知道的东西:元组有标签<代码>对于色调的元组{print(tuple.key)}
另一件有用的事情是这样做:
对于dict{print(key)}中的(key,object)
<代码>对于色调的元组{print(tuple.key)}
另一件有用的事情是这样做:
对于dict{print(key)}中的(key,object)
。这不是我的反对票,但我想这是因为缺乏研究。我通过命令单击
Dictionary
并在模块中搜索
iterator
找到了答案。我在这里处于劣势,因为我不知道搜索什么。我确实花了至少1/2个小时试图弄明白,但失败了。我没有意识到map函数依赖于Dictionary的迭代器。现在我知道这一点是有道理的,但我不知道该找什么。”\_(ツ)_/’这是堆栈溢出,我放弃了对向下投票的提问。如果我的问题或答案有点不足,我不介意向下投票——只要有人提出建设性的批评。但是向下投票的向下投票真的让我恼火。不是我的向下投票,但我猜这是因为缺乏研究。我通过点击命令找到了答案<代码>字典并在模块中搜索<代码>迭代器。我在这里处于劣势,因为我不知道搜索什么。我确实花了至少1/2个小时试图找出它,但失败了。我没有意识到映射函数依赖于字典的迭代器。现在我知道了这一点,但我不知道是什么寻找\_(ツ)_/'这是堆积如山的,我放弃了提问,放弃了,放弃了,再见,如果我的问题或答案是缺乏的,我不介意被否决——只要有人发表建设性的批评。但是,以低票驾驶真的让我很恼火。