<div dir="ltr">"print(toSort)" is only a placeholder there. Yes, it will be a visualization. I am thinking of saving each state of "toSort" 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"><<a href="mailto:gwk.lists@gmail.com" target="_blank">gwk.lists@gmail.com</a>></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'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'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 <<a href="mailto:erginbil@gmail.com" target="_blank">erginbil@gmail.com</a>> 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..<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 > 0) && (toSort[j-1] > 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"><<a href="mailto:gwk.lists@gmail.com" target="_blank">gwk.lists@gmail.com</a>></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>
><br>
> On Mar 23, 2016, at 12:35 PM, Ergin Bilgin via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:<br>
><br>
> Hello,<br>
><br>
> 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>
><br>
> while (i < 50){<br>
><br>
><br>
> print(i)<br>
><br>
> i<br>
> += 1<br>
><br>
> waitForMouseClick<br>
> () //Wait here for user input.<br>
> }<br>
> I also use Sprite Kit if you can think a solution related to it.<br>
><br>
> Cheers,<br>
><br>
> Ergin<br>
><br>
</div></div>> _______________________________________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
> <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>