<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">I test you code in command line `swiftc main.swift` and in macOS Command Line Tool app.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Here is the main.swift:</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">import</span><span style="font-variant-ligatures:no-common-ligatures"> Foundation</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">import</span><span style="font-variant-ligatures:no-common-ligatures"> Foundation</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">func</span><span style="font-variant-ligatures:no-common-ligatures"> input() -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">String</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(52,149,175)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> keyboard = </span><span style="font-variant-ligatures:no-common-ligatures">FileHandle</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">.</span><span style="font-variant-ligatures:no-common-ligatures">standardInput</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures"> inputData = keyboard.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">availableData</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures"> strData = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">String</span><span style="font-variant-ligatures:no-common-ligatures">(data: inputData, encoding: .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">utf8</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">return</span><span style="font-variant-ligatures:no-common-ligatures"> strData!</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">func</span><span style="font-variant-ligatures:no-common-ligatures"> write(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">_</span><span style="font-variant-ligatures:no-common-ligatures"> txt: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">String</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">print</span><span style="font-variant-ligatures:no-common-ligatures">(txt, terminator: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(180,38,26)">""</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">func</span><span style="font-variant-ligatures:no-common-ligatures"> read() -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">String</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(52,149,175)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> c = </span><span style="font-variant-ligatures:no-common-ligatures">CharacterSet</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">.</span><span style="font-variant-ligatures:no-common-ligatures">whitespacesAndNewlines</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(52,149,175)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">return</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">input</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">().</span><span style="font-variant-ligatures:no-common-ligatures">trimmingCharacters</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(in: c)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,143,0)"><span style="font-variant-ligatures:no-common-ligatures">/* main program */</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(180,38,26)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">write</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures">"Enter your name: "</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures"> s = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">read</span><span style="font-variant-ligatures:no-common-ligatures">()</span></p><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal">
</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(180,38,26)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">print</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures">"You name is </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">\</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">s</span><span style="font-variant-ligatures:no-common-ligatures">)"</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)</span></p><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(180,38,26)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"><br></span></p><p style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif" color="#000000">It turns out that the input request was ran before the `</font></span><font color="#000000" face="georgia, serif">write("Enter your name: ")`. I don't why. Maybe it is a bug?</font></p><p style="margin:0px;line-height:normal"><font color="#000000" face="georgia, serif"><br></font></p><p style="margin:0px;line-height:normal"><font color="#000000" face="georgia, serif">Here is my output in terminal:</font></p><p style="margin:0px;line-height:normal"><font color="#000000" face="georgia, serif"><br></font></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">$ swiftc main.swift </span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">$ ./main </span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">abc</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">Enter your name: You name is abc</span></p><p style="margin:0px;line-height:normal">
</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">$ </span></p><p style="margin:0px;line-height:normal"><font color="#000000" face="georgia, serif"><br></font></p><p style="margin:0px;line-height:normal"><font color="#000000" face="georgia, serif">As you can see, the program asked me to input my name before it showed the notification. In playground, the situation is alike. </font></p><p style="margin:0px;line-height:normal"><font color="#000000" face="georgia, serif"><br></font></p><p style="margin:0px;line-height:normal"><font color="#000000" face="georgia, serif">However, if the code is running as a command line tool, created by Xcode -> Create a new project -> macOS, command line tool, everything works fine.</font></p><p style="margin:0px;line-height:normal"><font color="#000000" face="georgia, serif"><br></font></p><p style="margin:0px;line-height:normal"><font color="#000000" face="georgia, serif">Zhaoxin</font></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Sep 18, 2016 at 9:16 PM, Mr Bee via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue-Light,Helvetica Neue Light,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif;font-size:16px"><div><span>Hi all,</span></div><div><span><br></span></div><div><span>Another question. </span>I used to use this snippet to read keyboard input from XCode's Playground. And it used to work very well. Today, I just updated my XCode to v.8 and Swift v.3. After a little bit modification here and there due to Swift 3 incompatibility, I got this code compiled without error. But it doesn't work. No keyboard input is taken. It just stucks.</div><div><br></div><div dir="ltr">Here's the code:</div><div dir="ltr"><br></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span style="color:#35568a">import</span><span> Foundation</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px"><span></span><br></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span style="color:#35568a">func</span><span> input() -> </span><span style="color:#c35900">String</span><span> {</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#35568a">let</span><span> keyboard = </span><span style="color:#c35900">FileHandle</span><span>.</span><span style="color:#587ea8">standardInput</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#35568a">let</span><span> inputData = keyboard.</span><span style="color:#587ea8">availableData</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#35568a">let</span><span> encoding = </span><span style="color:#c35900">String</span><span>.</span><span style="color:#c35900">Encoding</span><span>(rawValue: </span><span style="color:#c35900">String</span><span>.</span><span style="color:#c35900">Encoding</span><span>.</span><span style="color:#587ea8">utf8</span><span>.</span><span style="color:#587ea8">rawValue</span><span>)</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#35568a">let</span><span> strData = </span><span style="color:#c35900">String</span><span>(data: inputData, encoding: encoding)</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#35568a">return</span><span> strData! </span><span style="color:#35568a">as</span><span> </span><span style="color:#c35900">String</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px"><span></span><br></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span style="color:#35568a">func</span><span> write(</span><span style="color:#35568a">_</span><span> txt: </span><span style="color:#c35900">String</span><span>) {</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#587ea8">print</span><span>(txt, terminator: </span><span style="color:#e82300">""</span><span>)</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px"><span></span><br></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span style="color:#35568a">func</span><span> read() -> </span><span style="color:#c35900">String</span><span> {</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(88,126,168)"><span style="color:#000000"> </span><span style="color:#35568a">let</span><span style="color:#000000"> c = </span><span style="color:#c35900">CharacterSet</span><span style="color:#000000">.</span><span>whitespacesAndNew<wbr>lines</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(88,126,168)"><span style="color:#000000"> </span><span style="color:#35568a">return</span><span style="color:#000000"> </span><span>input</span><span style="color:#000000">().</span><span>trimmingCharacters</span><span style="color:#000000">(in: c)</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px">}<br></div><div dir="ltr"><span><br></span></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo"><span style="color:rgb(207,135,36)">/* main program */</span><br></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px"><span></span><br></div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(232,35,0)"><span style="color:#587ea8">write</span><span style="color:#000000">(</span><span>"Enter your name: "</span><span style="color:#000000">)</span></div><div dir="ltr">
</div><div style="margin-top:0px;margin-bottom:0px;font-size:12px;line-height:normal;font-family:Menlo" dir="ltr"><span style="color:#35568a">let</span><span> s = </span><span style="color:#587ea8">read</span><span>()</span></div><div></div><div> </div><div><div><div>So, does anyone know how to make it works (again)? Thank you.</div><div><br></div><div dir="ltr">Regards,</div></div><div><br></div>–Mr Bee<div><br></div></div></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>