【问题标题】:Swift - How do I create a string from array of objects that have String property?Swift - 如何从具有 String 属性的对象数组创建字符串?
【发布时间】:2018-06-26 07:19:46
【问题描述】:

如何从具有 String 属性的对象数组中创建字符串?

class Person {
   let name: String
}

let people = [Person(name: "Sam"), Person(name: "Zoey"), Person(name: "Bil")]

let peopleNames: String = //what should be here?

peopleNames = "Sam, Zoey, Bil"

【问题讨论】:

标签: ios swift string


【解决方案1】:

我想你想要"Sam, Zoey, Bil" 作为你的结果?

在这种情况下,您可以这样做:

people.map { $0.name }.joined(separator: ", ")

我们首先将所有人转换为他们的名字,然后调用joined,它将所有字符串连接在一起。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2022-11-15
    • 2019-07-30
    相关资源
    最近更新 更多