首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CommandLine.arguments与Processinfo.processinfo.arguments在swift中的区别

CommandLine.arguments与Processinfo.processinfo.arguments在swift中的区别
EN

Stack Overflow用户
提问于 2018-01-11 07:07:16
回答 1查看 1.7K关注 0票数 6

我想知道这些有什么不同,CommandLineProcessInfo

代码语言:javascript
复制
let elements = CommandLine.arguments
let elements = Processinfo.processinfo.arguments

在我看来,ProcessInfo的论点包含了Commandline的所有概念。因此,处理争论没有什么不同之处。

下面的代码使用CommandLine.arguments,用于练习读写文件。

如果我把Processinfo.processinfo.arguments放在CommanLine.arguments的位置。什么都没变。

代码语言:javascript
复制
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
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-11 07:25:28

CommandLine是Swift标准库的一部分,只提供命令行参数和参数计数。

ProcessInfo是基础框架的一部分(不是语言的一部分)。虽然ProcessInfo.arguments确实给出了与CommandLine.arguments相同的结果,但是ProcessInfo类有更多的结果。

虽然这两个arguments在功能上是相同的,但如果只需要命令行参数,则使用CommandLine。它更简单,它不依赖任何额外的框架,而且它将更易于移植到其他Swift运行时。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48201517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档