2015-02-10 42 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