The Hello, World program is typically the first program used to introduce beginners to programming. It demonstrates how to write, compile, and run a basic program in Swift.

Swift Hello World First Program

  • First, open any text editor. For example, use VSCode.

  • Next, open Visual Studio Editor and create a file named Hello.swift. Swift code files have the extension .swift.

  • Add the below code, and save it as Hello.swift


// Hello.swift
// Hello World program in Swift
import Foundation;

print("Hello World W3schools.io!");

The Hello World program comprises three components

Comments are text strings providing explanations about lines of code or Swift files.

They are ignored by the Swift compiler. Comments start with // followed by the comment text.

  • import

The import keyword is used to import namespaces containing variables and functions into the current code. The Foundation library is a basic library that provides utility classes in Swift.

  • print

print is the output function used to print the string to the console.

Compile and Run the Swift Program

Compile the code using the swiftc compiler

swiftc Hello.swift

or

swiftc Hello.swift -o Hello

This compiles the code and creates an executable file named Hello. You can customize the executable file name using the -o option.

You can run the code using

In Windows

Hello

In Linux or Mac

./Hello