Why does "for (index, (a, b)) in dict.enumerated ()" throw an error?

I tried to skip the dictionary:

let someDict = [...]
for (index, (a, b)) in someDict.enumerated() {

}

This shows me an error:

cannot express tuple conversion '(offset: Int, element: (key: String, value: String))' to '(Int, (String, String))'

This is really weird. Because if we compare the required tuple:

(offset: Int, element: (key: String, value: String))

with a tuple type in a for loop:

(Int, (String, String))

They are compatible!

Why is this happening?

Note, I understand that dictionaries do not have a specific order, so knowing the KVP index is not very useful. But I still want to know why this is not working.

+2
source share
1 answer

The Swift Programming Language (Swift 3) , .

:

  let someDict = [...]
  for tuple in someDict.enumerated() {
    let index = tuple.offset
    let a = tuple.element.key
    let b = tuple.element.value

  }

  var tuple: (offset: Int, element: (key: String, value: String))
0

Source: https://habr.com/ru/post/1657209/


All Articles