xcode 在 swift 操场上打破(意外)无限循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26745195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:05:35  来源:igfitidea点击:

Break an (accidental) endless loop in a swift playground

xcodehangswift-playground

提问by Ronny Webers

while playing around in a swift playground (what's in a name), I accidentally entered an endless loop, such like this one :

在 swift 的操场上玩耍时(名字有什么意思),我不小心进入了一个无限循环,比如这个:

var l = 3
while (l > 2) {
  println(l)
  l++
}

this causes the playground to endlessly print to the console, upon which Xcode gets stuck

这会导致 Playground 无休止地打印到控制台,而 Xcode 卡住了

The only way I found was to kill Xcode through the terminal window, however I would expect there is some more elegant way to 'stop' the playground from executing?

我发现的唯一方法是通过终端窗口杀死 Xcode,但是我希望有一些更优雅的方法来“阻止”操场执行?

回答by Sassan Sanei

Playground is operating exactly as designed, but it really should have a means of instantly halting execution while editing code. I have entered endless loops in mid-edit the same way as you, and it usually happens while editing the conditions in a for or while loop.

Playground 完全按照设计运行,但它确实应该有一种在编辑代码时立即停止执行的方法。我和你一样在编辑中进入无限循环,它通常发生在 for 或 while 循环中编辑条件时。

I work around this limitation by deliberately typing a few characters of gibberish on the line I am editing, or on a separate line if editing multiple lines. Playground will choke on the gibberish and stop executing the code. When I finish editing, I remove the gibberish so that Playground can execute the code once again.

我通过在我正在编辑的行上或在编辑多行时在单独的行上故意输入一些乱码来解决此限制。Playground 会因为这些乱码而窒息并停止执行代码。当我完成编辑时,我删除了乱码,以便 Playground 可以再次执行代码。

For example, if I want to edit this line:

例如,如果我想编辑这一行:

for var j=0;j<10000000;j=j+1000 {

I will first add gibberish to the end:

我将首先在最后添加胡言乱语:

for var j=0;j<10000000;j=j+1000 { adsklfasd

then I will make my edits:

然后我将进行编辑:

for var j=0;j<500;j=j+10 { adsklfasd

then i will remove the gibberish, leaving behind only the good code:

然后我将删除胡言乱语,只留下好的代码:

for var j=0;j<500;j=j+10 {

Playground won't execute as long as the adsklfasd is in there.

只要 adsklfasd 在那里,Playground 就不会执行。

The gibberish doesn't have to go at the end of the for statement; you could put it on a separate line, if you prefer.

胡言乱语不必出现在 for 语句的末尾;如果你愿意,你可以把它放在一个单独的行上。

It's not an elegant solution, but it's fast and easy and it works. Hope this helps.

这不是一个优雅的解决方案,但它快速、简单并且有效。希望这可以帮助。

回答by user5718828

I was able to halt my loop by typing the iteration variable setting (that would stop the loop) somewhere else like in a text editor then right click pasting the text into the loop.

我可以通过在文本编辑器等其他地方输入迭代变量设置(这将停止循环)来停止我的循环,然后右键单击将文本粘贴到循环中。

In your case, right-click pasting l = 0 right before the last bracket should work.

在您的情况下,在最后一个括号应该起作用之前右键单击粘贴 l = 0 。

回答by t1ser

This is Xcode 8.3.3. I did not check previous versions.

这是 Xcode 8.3.3。我没有检查以前的版本。

While the playground is running, the source code is still editable. Just change the loop, type in a command, for example breakinto the code, inside the loop. This is interpreted code not compiled code so it will take effect.

在 Playground 运行时,源代码仍然是可编辑的。只需更改循环,输入命令,例如在循环内闯入代码。这是解释代码而不是编译代码,所以它会生效。