If you want to split a string by a given character then you can use the
built-in split()
method, without needing Foundation:
let str = "Today is so hot"
let arr = split(str, { $0 == " "}, maxSplit: Int.max, allowEmptySlices: false)
println(arr) // [Today, is, so, hot]
Update for Swift 1.2: The order of the parameters changed with Swift 1.2 (Xcode 6.3), compare split now complains about missing "isSeparator":
let str = "Today is so hot"
let arr = split(str, maxSplit: Int.max, allowEmptySlices: false, isSeparator: { $0 == " "} )
println(arr) // [Today, is, so, hot]
Update for Swift 2: See Stuart's answer.
Update for Swift 3:
let str = "Today is so hot"
let arr = str.characters.split(separator: " ").map(String.init)
print(arr)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…