Swift 数组元素到变量

标签 swift vapor

我正在使用 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/

相关文章:

vapor - 了解如何初始化 Vapor 4 存储库

ios - 你能从 "drop"模型中将真实世界的坐标拉到 ARKit 的场景中吗

swift - 使用 Combine 合并先前的值

ios - 在 ios 中分别在一个 nsdate 中设置日期和时间时出现错误时间

swift - 如何向 Pivot 添加附加信息(使用 Fluent)?

swift - 获取 Vapor 中的 URI 片段

swift - Vapor 不通过 Leaf 渲染文件

json - 解析 api 响应 JSON 以获取内容值

swift - swift 3 中的后台任务

swift - 无法创建用于查询嵌套父项的 Fluent ORM 查询