First Swift Program
To run your first Swift program, you must have installed Xcode and Swift on your computer.
Open the Xcode playground and write down the following code:
1. // This is the first Swift program also called Hello, World! Program
The above line which is started with two slashes (//) is a comment. It is totally ignored by the compiler. Comments are used to make your code understandable by other person.
2. import Swift
The import Swift keyword is used to access all the symbols defined inside the Swift language. Without using this keyword, you can't use print statement to print anything on Xcode.
3. print("Hello, World!")
The above line is a print function in Swift. It is used to show the output on the screen.
The "Hello World" program on Terminal
Follow the steps given below:
- Open the terminal
- Type swift and press enter. You will get a welcome message as Welcome to Apple Swift version x.x.x.
- Type print("Hello, World!")
- Press Enter
Note: Unlike other programming language C, C++, Java, Python etc., Swift doesn't need to add a semicolon (;) at the end of the statement. It is optional.
You can add multiple lines in a single line by using semicolon (;) at the end of first statement.