How to convert dictionary to array

后端 未结 5 1691
闹比i
闹比i 2020-12-28 12:06

I want to convert my dictionary to an array, by showing each [String : Int] of the dictionary as a string in the array.

For example:     



        
5条回答
  •  借酒劲吻你
    2020-12-28 12:48

    If you like concise code and prefer a functional approach, you can use the map method executed on the keys collection:

    let array = Array(myDict.keys.map { "\($0) \(myDict[$0]!)" })
    

    or, as suggested by @vacawama:

    let array = myDict.keys.array.map { "\($0) \(myDict[$0]!)" }
    

    which is functionally equivalent

提交回复
热议问题