Swift Hello World Program
Printing “Hello World” message to output is the basic program through which one can get introduced to any programming language.
In this tutorial, we will learn how to print Hello World message to output.
First, let us create a Command Line Tool project in Xcode.
Open Xcode. A window appears as shown in the following screenshot.
Click on Create a new Xcode project.
Now, we have to choose a template for our project. To get started with learning Swift programming, we will create a Command Line Tool project.
Under macOS tab, select Command Line Tool template and click on Next button.
Give a name for the Product Name, and click on Next button.
Choose a Project location, and click on Create button.
The project is created with all the boiler plate code, to get started.
Open main.swift file in the Project navigator, present n the left panel of the Project window.
main.swift
Run this project, by clicking the Run (/Play) button present on the top of Left panel.
The build would be successful, with the following message displayed.
The build is successful, and the program is run. We can see the output in the All output window, as shown in the following screenshot.
We have successfully print the “Hello World” message to output.
Now, let us see what this main.swift program contains.
These first six lines of code are called comments. Comments are those parts of the code that are not executed, but helpful in the readability of the program. Each comment is started with a double forward slash.
import keyword is used to import namespaces that pack a set of symbols or functions. And importing a namespace would give us access to use those symbols or functions.
print() is a function. A function can have none, one or more parameters and transform them, and may or may not return the result. Here, print() function takes string "Hello, World!"
and does not return anything, but prints this string to output.
Conclusion
In this Swift Tutorial, we learned how to print "Hello, World!"
message to output.