[swift-users] reading keyboard input from xcode playgroud

Mr Bee pak.lebah at yahoo.com
Sun Sep 18 08:16:02 CDT 2016


Hi all,
Another question. 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.
Here's the code:
import Foundation
func input() -> String {  let keyboard  = FileHandle.standardInput  let inputData = keyboard.availableData  let encoding  = String.Encoding(rawValue: String.Encoding.utf8.rawValue)  let strData   = String(data: inputData, encoding: encoding)  return strData! as String}
func write(_ txt: String) {  print(txt, terminator: "")}
func read() -> String {  let c = CharacterSet.whitespacesAndNewlines  return input().trimmingCharacters(in: c)}

/* main program */

write("Enter your name: ")let s = read() So, does anyone know how to make it works (again)? Thank you.
Regards,
–Mr Bee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160918/af0c9bf8/attachment.html>


More information about the swift-users mailing list