How to get a dictionary key through an index?

Is there a way to access dictionary keys in Swift 2 using an index?

var dict = ["item1":1, "item2":2, "item3":3]
dict.keys[0]

leads to an error:

15:29: note: overloads for the "index" exist with these partially matching parameter lists: (Base.Index), (range), (Self.Index) print ("key =" + dict.keys [i])

I saw examples from August ( Swift: accessing a dictionary through an index ):

dict.keys.array[0]

At least in Swift 2 there are no array objects on dictionary keys.

+4
source share
2 answers

In Swift 2, the equivalent dict.keys.arraywould be Array(dict.keys):

let dict = ["item1":1, "item2":2, "item3":3]

let firstKey = Array(dict.keys)[0]  // "item3"

: , , " " .

+2

, , , / .

0

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


All Articles