2014-10-29 44 views
0
myText = "word 1/word 2"  
var testVar = split(myText, { $0 == "/"}, maxSplit: Int.max, allowEmptySlices: false) 

此代碼工作的一個亞型,但它需要空的空間「單詞1」的時候我用的testvar [0]斯威夫特 - 性格是不串

當我寫空的空間

var testVar = split(myText, { $0 == "/"}, maxSplit: Int.max, allowEmptySlices: false) 

我得到一個錯誤:'字符'不是'字符串'的子類型

任何人誰知道如何解決這個問題?

回答

2

split()函數僅適用於Swift字符串,比較字符串的每個元素爲Character。要使用一個字符串分割字符串,請使用.componentsSeparatedByString

var testVar = myText.componentsSeparatedByString("/") 
+0

大:)感謝ü:) – 2014-10-29 16:07:02

+0

此外,以供將來參考,'的最後兩個參數split'有默認值,所以你可以離開他們out:'var testVar = split(myText,{$ 0 ==「/」})'' – 2014-10-29 16:11:24