如果要按给定 字符 分割字符串,则可以使用内置
split()方法,而无需Foundation:
let str = "Today is so hot"let arr = split(str, { $0 == " "}, maxSplit: Int.max, allowEmptySlices: false)println(arr) // [Today, is, so, hot]Swift 1.2更新: 使用Swift 1.2(Xpre 6.3)更改了参数的顺序,比较[拆分现在抱怨缺少“
isSeparator”]
let str = "Today is so hot"let arr = split(str, maxSplit: Int.max, allowEmptySlices: false, isSeparator: { $0 == " "} )println(arr) // [Today, is, so, hot]Swift 更新:
let str = "Today is so hot"let arr = str.characters.split(separator: " ").map(String.init)print(arr)



