Hello World in Swift
Sure! Here's a simple "Hello, World!" program written in Swift:
swiftCopy code// hello_world.swift
// Print "Hello, World!" to the console
print("Hello, World!")
To run this Swift program, you can follow these steps:
Open a text editor or an Integrated Development Environment (IDE) that supports Swift, such as Xcode for macOS.
Create a new Swift file with the
.swift
file extension, and name ithello_world.swift
(or any other preferred name).Enter the above Swift code into the
hello_world.swift
file.Save the file.
Open a terminal and navigate to the directory where the
hello_world.swift
file is saved.Compile and run the Swift program by typing the following command in the terminal:
bashCopy codeswiftc hello_world.swift -o hello_world
./hello_world
The swiftc
command compiles the Swift source code into an executable binary file named hello_world
, and the ./hello_world
command runs the compiled program, which will print "Hello, World!" to the console.
Note: If you are using Xcode or an IDE with Swift support, you can also run Swift programs directly within the IDE's built-in compiler and debugger.