Sometimes its more convenient to run swift code in terminal. Specially when I implement data structure or test any algorithm, I like to write code in a text editor and run in terminal. Lets see how we can do it in macOS terminal:
1. How to check swift version
it shows the following output in my terminal
2. Writing a simple swift code and run in terminal
Run in terminal
3. Compiling swift code to executable file
If we want to compile 2 or more swift files together, we must have a file named main.swift which is the top level executable file. Its like main function in C or C++ or Java.
To compile and create an executable program:
swiftc a.swift main.swift -o prog
It will compile both a.swift and main.swift files linked together and create an executable file named prog.
To run the executable program in terminal: