xcode 如何在 Swift 游乐场中使用 STDIN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35380343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to take STDIN in Swift playground
提问by Vinod Rathod
I know that to program in STDIN and STDOUT, we need to make an command line project in Xcode. But how do I take a standard input in playground.
我知道要在 STDIN 和 STDOUT 中编程,我们需要在 Xcode 中创建一个命令行项目。但是我如何在操场上接受标准输入。
Whenever I try to run such code in playground
每当我尝试在操场上运行这样的代码时
var input = readLine()!
I always get this error
我总是收到这个错误
Execution was interrupted, reason: EXC_BAD_INSTRUCTION (Code=EXC_l386_INVOP, subcode=0x0)
执行被中断,原因:EXC_BAD_INSTRUCTION(代码=EXC_l386_INVOP,子代码=0x0)
Is it possible to take STDIN in playground or not?
是否可以在操场上使用 STDIN?
UPDATE
更新
I know this error is because of nilinput
variable but want to know how to overcome this nil value.
我知道这个错误是因为nilinput
变量,但想知道如何克服这个 nil 值。
回答by MANISH PATHAK
Fixed Solution for SWIFT 3
SWIFT 3 的固定解决方案
To make it work, Create a new command line tool project.
要使其工作,请创建一个新的命令行工具项目。
Go to "File" -> "New" -> "Project" -> "macOS" -> "Command Line Tool".
转到“文件”->“新建”->“项目”->“macOS”->“命令行工具”。
import Foundation
print("Hello, World!")
func solveMefirst(firstNo: Int , secondNo: Int) -> Int {
return firstNo + secondNo
}
func input() -> String {
let keyboard = FileHandle.standardInput
let inputData = keyboard.availableData
return NSString(data: inputData, encoding:String.Encoding.utf8.rawValue) as! String
}
let num1 = readLine()
let num2 = readLine()
var IntNum1 = Int(num1!)
var IntNum2 = Int(num2!)
print("Addition of numbers is : \(solveMefirst(firstNo: IntNum1!, secondNo: IntNum2!))")
And run the project using CMD + R
并使用运行项目 CMD + R
回答by EdiZ
Playground can not read an input from the commend line.
Playground 无法从推荐行读取输入。
You can use a custom "readLine()" function and a global input variable, each element in the input array is presenting a line:
您可以使用自定义“readLine()”函数和全局输入变量,输入数组中的每个元素都显示一行:
import Foundation
var currentLine = 0
let input = ["5", "5 6 3"]
func readLine() -> String? {
if currentLine < input.endIndex {
let line = input[currentLine]
currentLine += 1
return line
} else {
return nil
}
}
let firstLine = readLine() // 5
let secondLine = readLine() // 5 6 3
let thirdLine = readLine() // nil
回答by Henrik Gustafsson
Try using Optional Chaining:
尝试使用可选链接:
if let input = readLine() {
print("Input: \(input)")
} else {
print("No input.")
}
回答by Henrik Gustafsson
For getting input from command line, like Console.ReadLine... Chalkershas a solution as follows.
为了从命令行获取输入,例如 Console.ReadLine... Chalkers有如下解决方案。
func input() -> String {
var keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = keyboard.availableData
return NSString(data: inputData, encoding:NSUTF8StringEncoding) as! String
}
please ask for further if it doesn't work Vinod.
如果 Vinod 不起作用,请进一步询问。
回答by Eyad bassam
Go to
去
New > Project > MacOs > Command Line Tool
新建 > 项目 > MacOs > 命令行工具
then you can apply :
那么你可以申请:
let value1: String?
让值1:字符串?
value1 = readLine()
print(value ?? "")
"" for the default value
"" 为默认值