readline
In this tutorial, we’ll be discussing how to read the standard input in Swift from the user and the different ways to print the output onto the screen. Swift print() is the standard function to display an output onto the screen.
在本教程中,我们将讨论如何从用户中读取Swift中的标准输入以及将输出打印到屏幕上的不同方法。 Swift print()是在屏幕上显示输出的标准函数。
Let’s get started by creating a new command line application project in XCode.
让我们开始使用XCode创建一个新的命令行应用程序项目。
快速输入 (Swift Input)
In Swift, the standard input is not possible in Playgrounds. Neither in iOS Application for obvious reasons. Command Line Application is the possible way to read inputs from the user.
在Swift中,标准输入在Playgrounds中是不可能的。 出于明显的原因,iOS应用程序中都没有。 命令行应用程序是从用户读取输入的可能方法。
readLine()
is used to read the input from the user. It has two forms:
readLine()
用于从用户读取输入。 它有两种形式:
readLine()
: The default way.readLine()
:默认方式。readLine(strippingNewLine: Bool)
: This is default set to true. Swift always assumes that the newline is not a part of the inputreadLine(strippingNewLine: Bool)
:默认设置为true。 Swift始终假定换行符不是输入的一部分
readLine()
function always r