Read standard input from the console in Swift

A simple way to receive standard input from the console is using the readLine() function.

For example, we are to receive

1 2.33 str

from the console, the code can be:

let input = readLine()!.split(separator: " ")
let integerInput = Int(input[0])!
let doubleInput = Double(input[1])!
let stringInput = String(input[2])

And some explanation:

The readLine() function receives the input from the console as a String? type value. An exclamation mark '!' is added to guarantee that the return value is not nil.

the References:
(1) readline
(2) Swift console input (the easiest way out of the current study)

The split() function here returns an array of SubString type values.

The SubString type values can be converted into Int, Double, String and other type values using the corresponding initializers.

Note that readLine() cannot read standard input from the console in playgrounds. To read standard input, create an OS X -> choose Command Line Tool project.

references:
(3) readLine() in playgrounds

Guess you like

Origin www.cnblogs.com/Chunngai/p/11615805.html