如何在swift中通过另一个字符串值连接数组中的每个元素

Tus*_*ade -5 swift

我正在尝试使用 swift 5 将数组中的多个元素与另一个字符串连接起来:例如:

MyArray = ["Bat","Foot","Basket"]
MyString = "Ball"

output = ["BatBall","FootBall","BasketBall"]
Run Code Online (Sandbox Code Playgroud)

或者

output = ["BallBat","BallFoot","BallBasket"]
Run Code Online (Sandbox Code Playgroud)

我该怎么做?请帮忙。

小智 6

您可以通过地图功能实现这一点:

var myArray = ["Bat","Foot","Basket"]

var output = myArray.map { $0 + "ball" }

print(output) // ["Batball", "Football", "Basketball"]
Run Code Online (Sandbox Code Playgroud)