Swift - Convert Array to Dictionary

后端 未结 7 1519
轻奢々
轻奢々 2021-02-15 12:00

I just want convert an array of Player Names into a dictionary Scoreboard, giving everyone an initial score of 0.

Meaning...

var playerNames =          


        
7条回答
  •  被撕碎了的回忆
    2021-02-15 12:30

    Here's a quick one liner that I like to use:

    let scoreboard = playerNames.reduce(into: [String: Int]()) { $0[$1] = 0 }
    

提交回复
热议问题