我想知道这些有什么不同,CommandLine,ProcessInfo
let elements = CommandLine.arguments
let elements = Processinfo.processinfo.arguments在我看来,ProcessInfo的论点包含了Commandline的所有概念。因此,处理争论没有什么不同之处。
下面的代码使用CommandLine.arguments,用于练习读写文件。
如果我把Processinfo.processinfo.arguments放在CommanLine.arguments的位置。什么都没变。
static func makeInOutFile() -> (inputFile: String, outputFile: String)? {
let elements = CommandLine.arguments
let inputFile: String
let outputFile: String
switch elements.count {
case 2:
inputFile = elements[1]
outputFile = Message.ofDefaultJSONFileName.description
return (inputFile: inputFile, outputFile: outputFile)
case 3:
inputFile = elements[1]
outputFile = elements[2]
return (inputFile: inputFile, outputFile: outputFile)
default:
print (Message.ofFailedProcessingFile)
return nil
}
}发布于 2018-01-11 07:25:28
CommandLine是Swift标准库的一部分,只提供命令行参数和参数计数。
ProcessInfo是基础框架的一部分(不是语言的一部分)。虽然ProcessInfo.arguments确实给出了与CommandLine.arguments相同的结果,但是ProcessInfo类有更多的结果。
虽然这两个arguments在功能上是相同的,但如果只需要命令行参数,则使用CommandLine。它更简单,它不依赖任何额外的框架,而且它将更易于移植到其他Swift运行时。
https://stackoverflow.com/questions/48201517
复制相似问题