问题
I'm asking how to do this outside of Xcode.
A Swift script written in a text editor can be executed with xcrun swift hello_world.swift or swift hello_world.swift. They can also be set to executable and run with a shebang. This is parallel to a scripting language like Python.
But since Swift is a compiled language, I'm sure there has to be some way of building a Swift executable through the Terminal using swift, clang, etc. Has anyone found anything regarding this? I've surprisingly found nothing.
回答1:
You need swiftc:
% cat > hello.swift << EOF
heredoc> println("Hello, world!")
heredoc> EOF
% swiftc hello.swift
% ./hello
Hello, world!
%
If you want to compile multiple files, the one you want to actually run on launch needs to be called main.swift (in which case you probably also want to use -o executablename).
Various options available via swiftc --help. The most likely one you want to use is -O to turn on the optimizer.
Also, depending on your environment settings, you may need to use xcrun -sdk macosx swiftc, if you are using Foundation or other SDKs.
来源:https://stackoverflow.com/questions/30045495/how-do-i-build-an-executable-for-an-application-written-in-swift-from-the-termin