0

Does swift have an input keyword or method like Java or C# or C++ to take input from the user in the Xcode playground?

Wasif
  • 25
  • 5
  • 1
    Does this answer your question? [Input from the keyboard in command line application](https://stackoverflow.com/questions/24004776/input-from-the-keyboard-in-command-line-application) – Keshu R. Jan 11 '20 at 08:57

1 Answers1

0

Swift 5.1

Go to "File" -> "New" -> "Project" -> "macOS" -> "Command Line Tool".

import Foundation

func input() -> String {
    let keyboard = FileHandle.standardInput
    let inputData = keyboard.availableData
    return String(data: inputData, encoding:String.Encoding.utf8.rawValue)!
}

if let number1 = readLine() , let number2 = readLine() {


var IntNum1 = Int(number1)
var IntNum2 = Int(number2)
}
Keshu R.
  • 5,045
  • 1
  • 18
  • 38
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
  • why using `NSString` in Swift? And then downcasting to String. Use this instead: `return String(data: inputData, encoding: .utf8.rawValue)` – Keshu R. Jan 11 '20 at 09:35