(Swift for Beginners)
Swift “Hello World” Program
In this article, you will dive into Swift programming by writing a “Hello, World!” program. You’ll learn the basic syntax for a working Swift program.
A “Hello, World!” is a computer program that is often used to illustrate the basic syntax of a programming language with a working program. This program outputs Hello, World! on the screen.
If you want to run this program in your computer, make sure that Swift and Xcode is properly installed. For that, check How to run Swift on your computer?
Let’s explore how to create Swift “Hello, World!” program.
“Hello, World!” program on Xcode Playground
// Hello, World! Program
import Swift
print("Hello, World!")
Copy the exact code onto the Xcode Playground.
When you run the program, the output will be:
Hello, World!
How Swift “Hello, World!” Program Works?
// Hello, World! Program
In Swift, any line starting with two slashes//
is a comment. They are completely ignored by the compiler. Comments are intended for person reading the code to better understand the intent and functionality of the program. If you want to learn more about comments visit Swift Comments.import Swift
import
keyword allows you to access all the symbols defined inside a framework.For now, just remember this line is required to useprint("Hello, World!")
in our program. You’ll learn more about it in the later tutorials.print("Hello, World!")
The above line is called a function in Swift. More specifically, a print
function.
In swift, print means “show on the screen”. The above code prints the string inside quotation marks, i.e “Hello, World!” on the screen. You’ll learn more about functions and how they work in later tutorials.
“Hello, World!” Program on Terminal
- Open Terminal
- Type
swift
and press enter (return). This will give you a welcome message as Welcome to Apple Swift version x.x.x. - Type
print("Hello, World!")
When you hit the enter (or return) key, the output will be :
Hello, World!
Things to remember
- If you are familiar with other programming languages like C, C++, Java you need to add semicolon (;) at the end of each statement. However, it’s optional to add semicolon “;” at the end of statement in Swift. It is also not recommended.If you need to add multiple statements in a single line, you must include “;” at the end of the statement.Example:
print("Hello,");print("World!")
- If you wish, you can remove the line
import Swift
from your program because it’s automatically included in the playground for you.
Don’t worry if you don’t understand some concepts in this tutorial for now, we’ll discuss them in detail in our later tutorials.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.