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

Swift Introduction Program

In diesem Artikel werden Sie durch das Schreiben von "Hello,World!” die Swift-Programmierung vertiefen. Sie werden die grundlegenden Syntaxen des Swift-Programms lernen.

Lassen Sie uns den Einstieg in die Swift-Programmierung mit dem einfachsten "Hello,World!” beginnen und die grundlegenden Syntaxen der Swift-Programmiersprache erläutern. Das Programm gibt "Hello,World!” auf dem Bildschirm aus.

Um das Programm auf dem Computer auszuführen, stellen Sie sicher, dass Swift und Xcode korrekt installiert sind.

Lassen Sie uns erforschen, wie man ein Swift "Hello,World!”-Programm erstellt.

Programm "Hello,World!” auf dem Xcode Playground

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

Kopieren Sie den genauen Code in den Xcode Playground.

Wenn Sie das Programm ausführen, wird der Ausgabe:

Hello, World!

Syntaxerklärung des Programms "Hello, World!"

  1. // Hello, World! Programm
    In Swift beginnt jede Zeile, die mit zwei Schrägstrichen (") beginnt//Sind alle Kommentare. Sie werden vollständig vom Compiler ignoriert. Kommentare sind für Leser des Codes gedacht, um die Absicht und die Funktion des Programms besser zu verstehen. Wenn Sie mehr über Kommentare erfahren möchten, besuchen SieSwift Comments.

  2. import Swift
    Der Schlüsselwort "import" ermöglicht den Zugriff auf alle in der Bibliothek definierten Symbole. Erinnern Sie sich daran, dass Sie in unserem Programm diese Zeile print("Hello, World!") verwenden müssen. In den folgenden Tutorials werden Sie mehr über sie erfahren.

  3. print("Hello, World!")
    Diese Zeile wird in Swift als Funktion bezeichnet. Genauer gesagt, es ist eine Druckfunktion.

    Dieser Code gibt den String in Anführungszeichen aus, d.h. "Hello, World!" und schreibt ihn auf den Bildschirm. In den folgenden Tutorials werden Sie mehr über Funktionen und ihre Funktionsweise erfahren.

"Hello, World!" Terminal-Programm

  1. Offene Endstation

  2. Input swift and press the Enter key (Return). This will give you a welcome message such as “}} 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++Other 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 Swift statements is optional. It is also not recommended to do so.
    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.