【问题标题】:how can I join words from a String array to a single String, separated by a comma in Swift? [duplicate]如何将字符串数组中的单词连接到单个字符串,在 Swift 中用逗号分隔? [复制]
【发布时间】:2017-09-28 21:20:23
【问题描述】:

我的Swift 应用程序中有一组Strings。我想将它们显示在一个标签中,每个标签都以, 分隔。我试过这个:

for hashtag in placeHashtags {
        text = text + "\(hashtag), "
    }

if (placeHashtags.count > 0){
    let text1 = text.remove(at: text.index(before: text.endIndex-1))
    text = text1.description
}

(第二个if 是删除最后一个逗号),但是我在标签中看不到任何内容。 我该如何解决?

【问题讨论】:

标签: swift


【解决方案1】:

你应该这样写:

let array = ["Hi", "Hello", "How", "Are", "You", "Fine"]
let joined = array.joined(separator: ", ")

【讨论】:

  • 谢谢Pragnesh,我会尽快接受的:)
猜你喜欢
  • 2012-04-11
  • 1970-01-01
  • 2019-12-12
  • 2017-09-29
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多