Does swift have an input keyword or method like Java or C# or C++ to take input from the user in the Xcode playground?
Asked
Active
Viewed 1,050 times
1 Answers
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)
}
-
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