我想知道如何使用 Swift 和 Xcode 创建我自己的命令行或 shell 程序,如“cat”、“git”等...?
例如:
rod -a "hello world!" rod --version rod /list -e "bye world!"
提前致谢
最佳答案
最简单的方法可能如下所示:
- 打开终端窗口并导航到您选择的目录
通过输入终端命令创建一个空的 swift 文件
touch myFirstProgram.swift
Make this file executable by entering the terminal command
chmod +x myFirstProgram.swift
Open this directory in finder by entering the terminal command
open .
Double-click on myFirstProgram.swift to open the empty file in Xcode. Then, inside Xcode, write the following lines into your first swift program. The first line starts the swift interpreter, anything after that is simple swift code.
#!/usr/bin/env xcrun swift println("Hello world.")
保存程序后,您可以通过输入终端命令启动它
./myFirstProgram.swift
完成。
了解了这些步骤后,就可以出去自由探索了。例如,我在 http://practicalswift.com/2014/06/07/swift-scripts-how-to-write-small-command-line-scripts-in-swift/ .我发现的唯一过时的信息是 swift 程序的第一行应该如我在第 5 点中详述的那样(行尾没有“-i”)。
玩得开心!
关于macos - 如何在 Mac OS X 中创建我自己的 "shell"命令行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31846780/