Swift数组和字符串相互转换(更新至Swift 4.0)
2016-05-29 本文已影响2415人
冷大大_hawkleng
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 Dev for MacOS