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!
当我想在我传递给函数的块中打印参数的值时,我看到了Swift 1.2和2.0的不同之处。任何有助于理解正在发生的事情的帮助都是值得感激的!
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
}
1 个解决方案
#1
1
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:
但是如果你打开菜单的话,有Xcode 7的游戏场:
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:
在Xcode 6游戏场中,你可以通过显示助理编辑器来达到同样的效果:
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)
}
#1
1
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:
但是如果你打开菜单的话,有Xcode 7的游戏场:
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:
在Xcode 6游戏场中,你可以通过显示助理编辑器来达到同样的效果:
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)
}