Input from the keyboard in command line application
cocoa, command-line, macos, swift
Solution
I managed to figure it out without dropping down in to C:
My solution is as follows:
func input() -> String {
var keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = keyboard.availableData
return NSString(data: inputData, encoding:NSUTF8StringEncoding)!
}
More recent versions of Xcode need an explicit typecast (works in Xcode 6.4):
func input() -> String {
var keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = keyboard.availableData
return NSString(data: inputData, encoding:NSUTF8StringEncoding)! as String
}
Problem
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?