如何在Swift中正确打印块参数?
当我想要打印作为输入传递给函数的块中的参数值时,我在Swift 1.2和2.0的操场中看到以下差异。任何有助于了解正在发生的事情都将不胜感激如何在Swift中正确打印块参数?,swift,swift-playground,Swift,Swift Playground,当我想要打印作为输入传递给函数的块中的参数值时,我在Swift 1.2和2.0的操场中看到以下差异。任何有助于了解正在发生的事情都将不胜感激 func blockSample(myInput: String, myOutput: (answer: String) -> ()) { myOutput(answer: myInput) } blockSample("testthis") { (answer) -> () in print(answer) // This
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
}
与其他示例相反,您的第一个示例确实没有打印在操场的活动面板中 但如果打开菜单,使用Xcode 7游乐场: 查看/调试区域/显示调试区域 您将在控制台中看到所有内容都正确打印 在Xcode 6游乐场中,您可以通过显示助理编辑器来实现相同的功能: 查看/助理编辑器/显示助理编辑器 另外,请记住,在游乐场中,您可以通过在单独的一行中声明变量,强制在live面板中显示值:
blockSample("testthis") { (answer) -> () in
answer // this will force the live display of the value for 'answer'
print(answer)
}