简体   繁体   English

Swift 命令行工具 - 读取多行

[英]Swift Command Line Tool - Read multiple lines

I know the readLine() method, but if copy paste a text with more than one lines, only the first line will be retrieved.我知道readLine()方法,但是如果复制粘贴多行的文本,则只会检索第一行。

I would like to retrieve all the text that the user copy pastes.我想检索用户复制粘贴的所有文本。

Is it possible?可能吗?

You can call readLine() in a loop and exit the loop in a predefined way您可以在循环中调用readLine()并以预定义的方式退出循环

var input: [String] = []

print("Enter text, finish by entering Q")
while let line = readLine(strippingNewline: true) {
    if line.lowercased() == "q" { break }
    input.append(line)
}

print(input)

Example例子

Enter text, finish by entering Q输入文字,输入 Q 结束
a一个
b b
c c
q q
["a", "b", "c"] [“a”,“b”,“c”]
Program ended with exit code: 0程序以退出代码结束:0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM