我在swift操场上有一个奇怪的行为。
当我输入这几行代码
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!
最后一行代码没有编译。错误为:
表达式过于复杂,无法在合理的时间内解决;考虑将表达式分解为不同的子表达式
我是做错了什么,还是这是某种错误?println()的限制似乎是5个字符串串联?
你没有做错任何事。苹果是
问题出在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