首页 > 编程语言 > 详细

Read standard input from the console in Swift

时间:2019-10-01 20:00:43      阅读:91      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/Chunngai/p/11615805.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!