2015-02-10 44 views
2

我在swift操場內有一種奇怪的行爲。Println和String Concatention問題

當我輸入的代碼

println("test 1" + "test 2" + "test 3" + "test 4") //compiles 
println("test 1" + "test 2" + "test 3" + "test 4" + "test 5") //compiles 
println("test 1" + "test 2" + "test 3" + "test 4" + "test 5" + "test 6") //error! 

此行的最後一行代碼不能編譯。錯誤是:

Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

我做錯了什麼,或者這是某種錯誤?看起來println()的限制是5個字符串連接?

回答

2

你沒有做錯任何事情。蘋果是。

println函數是問題,而不是字符串連接。這給了我同樣的錯誤:

println(1 + 2 + 3 + 4 + 5 + 6) 

你可以解決它通過聲明自己的包裝:

func myprint<T>(x: T) { 
    println(x) 
} 

myprint(1 + 2 + 3 + 4 + 5 + 6) 
myprint("1" + "2" + "3" + "4" + "5" + "6") 
myprint("1" + "2" + "3" + "4" + "5" + "6" + "1" + "2" + "3" + "4" + "5" + "6") 

輸出:

21 
123456 
123456123456 
+0

我打得有點用Xcode的測試版6.3。看起來像蘋果公司解決了這個問題:) – nemke 2015-02-11 01:11:36

+0

@nemke恕我直言,沙子只是轉移。它可能已經修復了一些情況,但我在6.3 beta 3中有一些表達式仍然「太複雜」。我甚至有一個表達式在Xcode 6.2中運行良好,但是我必須將其分解,因爲它顯然「太複雜了「爲6.3。 (嘆。) – Rob 2015-03-17 20:59:16