I am attempting to get the keyboard input for a command line app for the new Apple programming language Swift.
I've scanned the docs to no avail.
import Foundation
println("What is your name?")
???
Any ideas?
I am attempting to get the keyboard input for a command line app for the new Apple programming language Swift.
I've scanned the docs to no avail.
import Foundation
println("What is your name?")
???
Any ideas?
Another alternative is to link libedit for proper line editing (arrow keys, etc.) and optional history support. I wanted this for a project I'm starting and put together a basic example for how I set it up.
Usage from swift
ObjC wrapper to expose libedit
Here is simple example of taking input from user on console based application: You can use readLine(). Take input from console for first number then press enter. After that take input for second number as shown in the image below:
Since there were no fancy solutions to this problem, I made a tiny class to read and parse the standard input in Swift. You can find it here.
Example
To parse:
You do:
I just wanted to comment (I have not enough reps) on xenadu's implementation, because
CChar
in OS X isInt8
, and Swift does not like at all when you add to the array whengetchar()
returns parts of UTF-8, or anything else above 7 bit.I am using an array of
UInt8
instead, and it works great andString.fromCString
converts theUInt8
into UTF-8 just fine.However this is how I done it