如何从具有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"
我想你想要"Sam, Zoey, Bil"
你的结果吗?
在这种情况下,您可以执行以下操作:
people.map { $0.name }.joined(separator: ", ")
我们首先将所有人换成他们的名字,然后调用joined
将所有字符串连接在一起。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句