我想知道如何用Swift和Xcode创建自己的命令行或shell程序,类似于"cat"、"git"等。
例如:
rod -a "hello world!" rod --version rod /list -e "bye world!"
谢谢!
我想知道如何用Swift和Xcode创建自己的命令行或shell程序,类似于"cat"、"git"等。
例如:
rod -a "hello world!" rod --version rod /list -e "bye world!"
谢谢!
Create an empty swift file by entering the terminal command
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.")
After saving your program, you can start this by entering the terminal command
./myFirstProgram.swift
完成。
一旦您理解了这些步骤,就可以自由地探索。例如,我在http://practicalswift.com/2014/06/07/swift-scripts-how-to-write-small-command-line-scripts-in-swift/上找到了一个很好的第一个教程。我发现那里唯一过时的信息是Swift程序的第一行应该如我在第5点中详细说明的那样(不要在行末加上“-i”)。
祝玩得开心!