English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Swift Introduction Program

In this article, you will delve into Swift programming by writing "Hello, World!" and learn the basic syntax of Swift programs.

Let's start our Swift programming journey with the simplest "Hello, World!" and explain the basic syntax of the Swift programming language. The program outputs "Hello, World!" to the screen.

If you want to run the program on the computer, make sure Swift and Xcode are installed correctly.

Let's explore how to create a Swift "Hello, World!" program.

The "Hello, World!" program on the Xcode Playground

// Hello, World! Program
import Swift
print("Hello, World!")

Copy the exact code into the Xcode Playground.

When running the program, the output is:

Hello, World!

Syntax explanation of the "Hello, World!" program

  1. // Hello, World! Program
    In Swift, any line that starts with two slashes//They are all comments. They are completely ignored by the compiler. Comments are for the readers of the code to better understand the intention and function of the program. If you want to learn more about comments, please visitSwift Comments.

  2. import Swift
    The import keyword allows you to access all symbols defined within the framework. Now, remember that you must use this line print("Hello, World!") in our program. You will learn more about it in the following tutorials.

  3. print("Hello, World!")
    The above line is called a function in Swift. More specifically, it is a print function.

    The above code prints the string within quotes, that is, "Hello, World!". And outputs it to the screen. In the following tutorials, you will learn more about functions and how they work.

"Hello, World!" Terminal Program

  1. Open Terminal

  2. Input swift and press the Enter key (Return). This will give you a welcome message like “}} Welcome to Apple Swift version x.x.x.".

  3. Input print("Hello, World!")

When you press the Enter key (or Return key), the output is:

Hello, World!

Things to Remember

  1. If you are familiar with C, C++In programming languages such as Java, you need to add a semicolon (;) at the end of each statement. However, adding a semicolon (;) at the end of a Swift statement is optional and not recommended.
    If you need to add multiple statements on one line, you must include a semicolon (;) at the end of the statement.
    For example:

    print("Hello,");print("World!")
  2. If necessary, you can remove the line from the program by importing Swift, as it will be automatically included in the playground.

If you are not familiar with some concepts in this tutorial, please do not worry, we will discuss them in detail in future tutorials.