如何在 Swift 中访问命令行应用程序的命令行参数?
最佳答案
2017 年 1 月 17 日更新:更新了 Swift 3 的示例。Process
已重命名为 CommandLine
。
2015 年 9 月 30 日更新:更新示例以在 Swift 2 中运行。
实际上可以在没有 Foundation 或 C_ARGV
和 C_ARGC
的情况下做到这一点。
Swift 标准库包含一个 CommandLine
结构体,它包含一个名为 arguments
的 String
集合。所以你可以像这样打开参数:
for argument in CommandLine.arguments {
switch argument {
case "arg1":
print("first argument")
case "arg2":
print("second argument")
default:
print("an argument")
}
}
关于macos - 如何在 Swift 中访问命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24029633/