码迷,mamicode.com
首页 > 编程语言 > 详细

Read standard input from the console in Swift

时间:2019-10-01 20:12:27      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:init   stand   lan   fun   comm   mat   turn   int   adl   

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

标签:init   stand   lan   fun   comm   mat   turn   int   adl   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!