<div dir="ltr">&quot;print(toSort)&quot; is only a placeholder there. Yes, it will be a visualization. I am thinking of saving each state of &quot;toSort&quot; to another array and show a fake visualization after sorting is finished. Using stdin also might be a good solution but not very presentable.<div><br></div><div>Ergin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 23 March 2016 at 19:55, George King <span dir="ltr">&lt;<a href="mailto:gwk.lists@gmail.com" target="_blank">gwk.lists@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I don&#39;t know of an easy way to block the main thread and wait for GUI input, without disrupting Cocoa. If you do not need the GUI, then I suggest using the debugger to set a breakpoint; in general it&#39;s worth the effort to learn to use the debugger, rather than just print statements. Alternatively, you could read a line from stdin, and then step through your algorithm by hitting return in the terminal. If you are trying to visualize your algorithm it gets trickier to do so without restructuring it to operate within the runloop.<div><div class="h5"><div><br></div><div><div><blockquote type="cite"><div>On Mar 23, 2016, at 1:48 PM, Ergin Bilgin &lt;<a href="mailto:erginbil@gmail.com" target="_blank">erginbil@gmail.com</a>&gt; wrote:</div><br><div><div dir="ltr">Thank you for help. Maybe I have over simplified my problem. In my first example, your advice was totally fine. But when I want to do something more complex, I could not figure out how to use it. For example, I want to print each step in my insertion sort. Like this:<div><br></div><div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">for i in 1..&lt;toSort.count{</font></div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">    var j: Int = i</font></div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">    while ((j &gt; 0) &amp;&amp; (toSort[j-1] &gt; toSort[j])){</font></div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">        let temp: Int = toSort[j]</font></div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">        toSort[j] = toSort[j-1]</font></div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">        toSort[j-1] = temp</font></div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">        j--</font></div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">        print(toSort)</font></div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">        //Wait here.</font></div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">    }   </font></div><div><font face="monospace, monospace" style="background-color:rgb(238,238,238)">}</font></div></div><div><br></div><div>I am looking for a solution without tearing the sorting algorithm into pieces. (If it is possible.) </div><div><br></div><div>Ergin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 23 March 2016 at 18:52, George King <span dir="ltr">&lt;<a href="mailto:gwk.lists@gmail.com" target="_blank">gwk.lists@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Ergin,<br>
Are you familiar with how events are delivered via the application runloop? Essentially, you should not create a top-level loop that waits for input; the application runloop does this for you. If you want to accumulate 50 clicks, create the counter variable in the appropriate NSResponder (or UIResponder on iOS), e.g. your root NSView or your NSViewController. Then override `func mouseDown(event: NSEvent)` and increment the counter there.<br>
Hope that helps,<br>
George<br>
<div><div><br>
<br>
&gt;<br>
&gt; On Mar 23, 2016, at 12:35 PM, Ergin Bilgin via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; Hello,<br>
&gt;<br>
&gt; I have a very simple while loop and I want to wait for a mouse click(can be a different input, not important) between every step.  What I want to achieve is something like this:<br>
&gt;<br>
&gt; while (i &lt; 50){<br>
&gt;<br>
&gt;<br>
&gt; print(i)<br>
&gt;<br>
&gt;     i<br>
&gt; += 1<br>
&gt;<br>
&gt;     waitForMouseClick<br>
&gt; () //Wait here for user input.<br>
&gt; }<br>
&gt; I also use Sprite Kit if you can think a solution related to it.<br>
&gt;<br>
&gt; Cheers,<br>
&gt;<br>
&gt; Ergin<br>
&gt;<br>
</div></div>&gt; _______________________________________________<br>
&gt; swift-users mailing list<br>
&gt; <a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br>
</blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></div></blockquote></div><br></div>