Search code examples
swiftprintlnxcode6.1.1

Println and String Concatention issue


I have a strange behavior within swift playground.

When I enter this lines of code

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!

The last line of code does not compile. The error is:

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

Am I doing something wrong, or is this some kind of a bug? It seems like the limit for println() is 5 string concatenations?


Solution

  • You're not doing anything wrong. Apple is.

    The println function is the problem, not string concatenation. This gives me the same error:

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

    You can work around it by declaring your own wrapper:

    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")
    

    Output:

    21
    123456
    123456123456