I see the following discrepancy in Playgrounds for Swift 1.2 and 2.0 when I want to print the value of a parameter in a block I am passing as input to a function. Any help in understanding what is going on would be appreciated!
func blockSample(myInput: String, myOutput: (answer: String) -> ()) {
myOutput(answer: myInput)
}
blockSample("testthis") { (answer) -> () in
print(answer) // This should print "testthis" but it doesn't
}
blockSample("testthis") { (answer) -> () in
print("test") // print something before the next line
print(answer) // this works. prints "testthis"
}
blockSample("testthis") { (answer) -> () in
let printedAnswer = answer
print(answer) // this works. prints "testthis". Note that I am printing answer and not printedAnswer
}
Your first example indeed doesn't print in the live panel of the Playground contrary to the others.
But with Xcode 7 Playgrounds if you open the menu:
View / Debug Area / Show Debug Area
you will see in the console that everything is printed properly.
In Xcode 6 Playgrounds you can achieve the same by displaying the Assistant Editor:
View / Assistant Editor / Show Assistant Editor
Also, remember that in Playgrounds you can force the display of a value in the live panel by just stating your variable on a separate line:
blockSample("testthis") { (answer) -> () in
answer // this will force the live display of the value for 'answer'
print(answer)
}