从 Swift 中的名称元组数组中提取所有值

Extracting all values from an array of names tuples in Swift

我觉得这段代码有点冗长。有没有更好的方法从命名元组数组中提取所有名称参数?

这是一个玩具示例。我们从 [(player : String, score : Int)].

类型的 Swift 数组中提取了 allPlayersallScores
var info : [(player : String, score : Int)] = Array()
let value1 = (player: "A", score: 1)
let value2 = (player: "B", score: 2)
let value3 = (player: "C", score: 3)
info.append(value1)
info.append(value2)
info.append(value3)

var allPlayers : [String] = Array()
var allScores : [Int] = Array()
for (player, score) in info {
    allPlayers.append(player)
    allScores.append(score)
}

您可以使用地图功能来做到这一点:

var names = info.map {
  [=10=].player
}
// names = ["A", "B", "C"]