【发布时间】:2016-12-07 05:22:22
【问题描述】:
在一个应该加载场景的按钮中,我正在尝试学习使用 guard 语句,但对它在四个“转义”中的每一个中的作用感到非常困惑。并且不知道我应该如何处理没有场景的情况。
此处使用哪个正确: continue、return、break 或 throw?
还有……为什么?
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
if self.rr.contains(location) {
guard let nextScene = goesTo
else {print(" No such Scene ")
continue } // continue, return, break, throw !!!!????
loadScene(withIdentifier: nextScene)
}
}
}
【问题讨论】:
-
在你的情况下,为什么不使用 if let 而不是 guard let!在 else 下,你可以轻松地继续它。我觉得那个位置不适合用guard let!
-
因为: 1 我想学习如何在我已经了解的流程中使用保护语句。 2.如果找不到那个场景,我希望发生一些激烈的事情,我认为这是警卫的正确位置......防范灾难......@Fay007
-
guard 语句结构通常带有return,表示如果nextScene 不存在,则返回并且什么也不做,如果存在,现在可以使用nextScene 值(这在if 中是不可能的)让)。您不能使用“继续”或“中断”,因为它们只能在循环或 switch 语句中使用。
-
@Mina 所以返回语句返回到
if self.rr.contains...范围? -
@Confused,是的,return 只返回保护语句。指针将位于 'loadScene(withIdentifier: nextScene)' 。这个链接很有帮助->ericcerney.com/swift-guard-statement
标签: swift sprite-kit skscene