Swift hello world
From Nix-Pro
Running Swift code in terminal
- Check Swift version
swift --version
- Create hello world programm
vim test.swift print("Hello World")
- Run the program in terminal
swift test.swift
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:
- file1: a.swift
func helloWorld(){ print("Hello World") }
- file2: main.swift
func hello(){ helloWorld() } hello()
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:
./prog