Read standard input from the console in Swift

Posted chunngai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

references:
(1) readline
(2) Swift控制台输入(目前研究出来的最简单的办法)

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

以上是关于Read standard input from the console in Swift的主要内容,如果未能解决你的问题,请参考以下文章

(standard input): No keywords in input file

linux --stdin 管道输出

echo “新密码”|passwd --stdin 用户名

read IEEE standard for verilog

read IEEE standard for verilog

(转)pd.read_csv之OSError: Initializing from file failed的解决方案