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 nil input
variable but want to know how to overcome this nil value.
我知道这个错误是因为nil输入变量但是想知道如何克服这个nil值。
5 个解决方案
#1
3
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运行项目
#2
2
Try using Optional Chaining:
尝试使用可选链接:
if let input = readLine() {
print("Input: \(input)")
} else {
print("No input.")
}
#3
2
Playground can not read an input from the commend line.
游乐场无法从推荐线读取输入。
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
#4
0
For getting input from command line, like Console.ReadLine... Chalkers has 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。
#5
-1
Go to
New > Project > MacOs > Command Line Tool
新建>项目> MacOs>命令行工具
then you can apply :
然后你可以申请:
let value1: String?
让value1:String?
value1 = readLine()
print(value ?? "")
"" for the default value
“”表示默认值
#1
3
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运行项目
#2
2
Try using Optional Chaining:
尝试使用可选链接:
if let input = readLine() {
print("Input: \(input)")
} else {
print("No input.")
}
#3
2
Playground can not read an input from the commend line.
游乐场无法从推荐线读取输入。
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
#4
0
For getting input from command line, like Console.ReadLine... Chalkers has 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。
#5
-1
Go to
New > Project > MacOs > Command Line Tool
新建>项目> MacOs>命令行工具
then you can apply :
然后你可以申请:
let value1: String?
让value1:String?
value1 = readLine()
print(value ?? "")
"" for the default value
“”表示默认值