2
我通过一本字典试图循环:为什么dict.enumerated()中的“for(index,(a,b))”会产生错误?
let someDict = [...]
for (index, (a, b)) in someDict.enumerated() {
}
它显示了我的错误:
cannot express tuple conversion '(offset: Int, element: (key: String, value: String))' to '(Int, (String, String))'
这是很奇怪的。因为如果我们比较所需的元组:在与元组类型
(offset: Int, element: (key: String, value: String))
for循环:
(Int, (String, String))
他们是兼容!
这是怎么发生的?
请注意,我知道字典没有特定的顺序,因此知道KVP的索引不是很有用。但我仍然想知道为什么这不起作用。
我不能告诉*为什么*,但'为(index,(key:a,value:b))在someDict.enumerated()中有效。 –
这可能与[SR-922](https://bugs.swift.org/browse/SR-922)有关,其中类型检查程序有时无法匹配相同类型的元组,但标签不同。 – Hamish
@Hamish但是如果我使用'[Double:String]'这样的字典,它仍然无法编译! – Sweeper