CommandLine.arguments 和 Processinfo.processinfo.arguments 在 swift 中的区别
Difference between CommandLine.arguments and Processinfo.processinfo.arguments in swift
我想知道它们有何不同
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
}
}
CommandLine
是 Swift 标准库的一部分,仅提供命令行参数和参数计数。
ProcessInfo
是 Foundation 框架的一部分(不是语言的一部分)。虽然 ProcessInfo.arguments
确实为您提供了与 CommandLine.arguments
相同的结果,但 ProcessInfo
class.
的结果更多
虽然这两个 arguments
在功能上是相同的,但如果您只需要命令行参数,请使用 CommandLine
。它更简单,它不依赖于任何额外的框架,而且它更容易移植到其他 Swift 运行时。
我想知道它们有何不同 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
}
}
CommandLine
是 Swift 标准库的一部分,仅提供命令行参数和参数计数。
ProcessInfo
是 Foundation 框架的一部分(不是语言的一部分)。虽然 ProcessInfo.arguments
确实为您提供了与 CommandLine.arguments
相同的结果,但 ProcessInfo
class.
虽然这两个 arguments
在功能上是相同的,但如果您只需要命令行参数,请使用 CommandLine
。它更简单,它不依赖于任何额外的框架,而且它更容易移植到其他 Swift 运行时。