gpt4 book ai didi

linux - 如何在 Swift for Linux 中读取用户输入或标准输入?

转载 作者:IT王子 更新时间:2023-10-29 00:26:10 24 4
gpt4 key购买 nike

如何在 Swift for Linux 中读取用户输入或来自标准输入的字符流?

最佳答案

readLine()适用于 Ubuntu 15:

[Readline] Returns Characters read from standard input through the end of the current line or until EOF is reached, or nil if EOF has already been reached.

例子:

print("\nEnter your name:\n")
if let name = readLine() {
print("\nHello \(name)!\n")
}

ed@swiftux:~/Swift/Scripts$ ./testReadline

Enter your name:

Eric

Hello Eric!

readline() 也适用于 |(管道):

ed@swiftux:~/Swift/Scripts$ echo "Mike" | ./testReadline

Enter your name:

Hello Mike!


我也尝试过 NSFileHandle 的经典方式但尚未实现:

fatal error: availableData is not yet implemented: file Foundation/NSObjCRuntime.swift

关于linux - 如何在 Swift for Linux 中读取用户输入或标准输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34116224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com