I am new to Objective-C and XCode and I'm trying to learn something new. Is there any way to read string from stdin using swift ? Something like cin in C++ ?
Asked
Active
Viewed 9,111 times
8
-
Duplicate of http://stackoverflow.com/questions/24004776/input-from-the-keyboard-in-command-line-application – Jukka Suomela Jun 05 '14 at 14:49
1 Answers
11
Just as with Objective-C, you can use NSFileHandle
:
let standardInput = NSFileHandle.fileHandleWithStandardInput()
let input = standardInput.availableData

Austin
- 5,625
- 1
- 29
- 43
-
Thank you. This worked for me, but I didn't think that it did at first because it only worked sometimes for me. 1) From within Xcode, which seems to me to have numerous problems with doing input from the console, it works for all of my keys, except for my digit keys from the ten-key pad, 2) From Terminal, all keys seem to work. – Kaydell Jul 31 '14 at 09:25
-
Here's what I got working: http://66.36.184.53/programming/languages/apple/swift/tutorials/swift-for-java-developers/io_reading-from-the-console – Kaydell Jul 31 '14 at 11:14
-
@kaydell that link does not seem to work. any chance you can post the code as an answer? much appreciated. – Kinjal Dixit Apr 13 '16 at 05:45
-
@KinjalDixit, sorry for the bad link. What I've decided to do inside of Xcode, is just to use the digit keys across the top of the keyboard, and use the Return key, rather than trying to use the 10-key pad. When you deploy a command-line program to Terminal.app, all of the keys should work OK. – Kaydell Apr 14 '16 at 12:40