It's great to try out Swift code at the command line using the REPL, but what we really require is the ability to compile our code into an executable binary that we can run on demand. Let's take our "Hello world!" example and compile it into a binary:
- Open your favorite text editor and save the following into a file called HelloWorld.swift:
print("Hello world!")
- From the command line, in the folder that contains our Swift file, we can compile our binary using swiftc. We can specify the file or files to compile and use the -o flag to provide a name for the output binary:
swiftc HelloWorld.swift -o HelloWorld
- Now, we can run the binary:
> ./HelloWorld
> Hello world!
Compiling one file is great, but to perform any useful work, we are likely to have multiple Swift files that define things like models, controllers, and other logic, so how can we compile them into a single, executable binary?
When we have multiple files, we need...