我正在使用 Vapor 制作一个网络应用程序。
我有很多东西,例如:
["thing", "other thing", "third thing"]
我想创建具有相同名称但 ""
替换为结构中的 "+"
的空字符串变量。
我不能使用字典或数组,因为 vapor 需要字符串中的单个变量。我不能手动编写它们的原因是我希望能够将东西添加到我的数组中,而不必在其他地方也添加它们。
我可以自己替换,我只需要知道是否可以从数组元素中创建变量。
我是 Swift 的新手,我不知道从哪里开始。
感谢帮助
最佳答案
let array = ["thing", "other thing", "third thing"]
let newArray = array.map { $0.replacingOccurrences(of: " ", with: "+") }
print(newArray)
// ["thing", "other+thing", "third+thing"]
关于Swift 数组元素到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57269353/