Swift数组和字符串相互转换(更新至Swift 4.0)

Swift 4.0

数组转字符串

let array = ["a", "b", "c"]
let s = array.joined(separator: ",")
print(s)
// 结果为:a,b,c

Swift 2.0

字符串转数组

let str = "1,2,3"
let array = str.componentsSeparatedByString(",")

数组转字符串

let array = ["1", "2", "3"]
let str = ",".join(array)
// or
let str = array.joinWithSeparator(",")
Swift数组和字符串相互转换(更新至Swift 4.0)_第1张图片
Swift Dev for MacOS

你可能感兴趣的:(Swift数组和字符串相互转换(更新至Swift 4.0))