Python readline,readline_Swift readLine(),Swift print()

 2023-11-19 阅读 24 评论 0

摘要:readlinePython 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.

readline

Python 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()是在屏幕上顯示輸出的標準函數。

readline()。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 input

    readLine(strippingNewLine: Bool) :默認設置為true。 Swift始終假定換行符不是輸入的一部分

readLine() function always returns an Optional String by default.

默認情況下, readLine() 函數始終返回可選 字符串 。

Add the following line in your main.swift file.

main.swift文件中添加以下行。

let str = readLine() //assume you enter your Name
print(str) //prints Optional(name)

To unwrap the optional we can use the following code:

要打開可選的包裝,我們可以使用以下代碼:

if let str = readLine(){
print(str)
}

讀取Int或Float (Reading an Int or a Float)

To read the input as an Int or a Float, we need to convert the returned String from the input into one of these.

要將輸入讀取為Int或Float,我們需要將輸入返回的String轉換為其中之一。

if let input = readLine()
{if let int = Int(input){print("Entered input is \(int) of the type:\(type(of: int))")}else{print("Entered input is \(input) of the type:\(type(of: input))")}
}

For Float, replace Int initialiser with the Float.

對于Float,將Int 初始化程序替換為Float。

讀取多個輸入 (Reading Multiple Inputs)

The following code reads multiple inputs and also checks if any of the input is repeated.

以下代碼讀取多個輸入,還檢查是否重復了任何輸入。

while let input = readLine() {guard input != "quit" else {break}if !inputArray.contains(input) {inputArray.append(input)print("You entered: \(input)")} else {print("Negative. \"\(input)\" already exits")}print()print("Enter a word:")
}

The guard let statement is used to exit the loop when a particular string is entered
print() as usual prints the output onto the screen. The current input is checked in the array, if it doesn’t exist it gets added.

輸入特定字符串時,將使用guard let語句退出循環
與往常一樣, print()將輸出打印到屏幕上。 在數組中檢查當前輸入,如果不存在,則將其添加。

A sample result of the above code when run on the command line in Xcode is given below.

swift readline, swift print

下面給出了在Xcode的命令行上運行時上述代碼的示例結果。

讀取由空格分隔的輸入 (Reading Input Separated by Spaces)

The following code reads the inputs in the form of an array separated by spaces.

以下代碼以空格分隔的數組形式讀取輸入。

let array = readLine()?.split {$0 == " "}.map (String.init)if let stringArray = array {print(stringArray)
}

split function acts as a delimiter for spaces. It divides the input by spaces and maps each of them as a String. Finally they’re joined in the form of an array.

split函數充當空格的定界符。 它將輸入除以空格并將每個映射為String。 最后,它們以數組的形式加入。

讀取2D陣列 (Reading a 2D Array)

The following code reads a 2D Array

以下代碼讀取2D數組

var arr = [[Int]]()
for _ in 0...4 {var aux = [Int]()readLine()?.split(separator: " ").map({if let x = Int($0) {aux.append(x)}else{print("Invalid")}})arr.append(aux)
}print(arr)

In the above code we can create 4 sub arrays inside an array.
If at any stage we press enter without entering anything in the row, it’ll be treated as a empty subarray.

在上面的代碼中,我們可以在一個數組內創建4個子數組。
如果在任何階段我們按Enter鍵但未在行中輸入任何內容,則它將被視為空子數組。

迅捷print() (Swift print())

We’ve often used print() statement in our standard outputs.

我們經常在標準輸出中使用print()語句。

The print function actually looks like this:

打印功能實際上如下所示:

print(_:separator:terminator:)

print(_:separator:terminator:)

print() statement is followed by a default new line terminator by default

默認情況下, print()語句后接默認的新行終止符

print(1...5)  Prints "1...5"print(1.0, 2.0, 3.0, 4.0, 5.0) //1.0 2.0 3.0 4.0 5.0print("1","2","3", separator: ":") //1:2:3for n in 1...5 {print(n, terminator: "|")
}
//prints : 1|2|3|4|5|
  • terminator adds at the end of each print statement.

    終止符在每個打印語句的末尾添加。
  • separator adds between the output values.

    分隔符在輸出值之間相加。

Concatenating string with values
We use \(value_goes_here) to add values inside a string.

將字符串與值連接
我們使用\(value_goes_here)在字符串中添加值。

var value = 10
print("Value is \(value)") // Value is 10

This brings an end to this tutorial on Swift Standard Input and Output.

這樣就結束了有關Swift標準輸入和輸出的本教程。

翻譯自: https://www.journaldev.com/19612/swift-readline-swift-print

readline

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/183201.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息