Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Swift Dictionary

  1. Home
  2. Swift Dictionary
  3. Creating and Running .swift Files

Creating and Running .swift Files

This page explains how to save Swift code as a text file and run it. The file itself is simply a plain text file saved with a .swift extension.

How to write a .swift file

Write your Swift code in a text editor and save the file with a .swift extension. Save the file using the UTF-8 character encoding.

sample_hello.swift
import Foundation

let name = "Swift"
let version = 5

print("Language: \(name)")
print("Version: \(version)")
print("Hello, World!")

Swift allows you to write code directly at the top level and run it like a script. You can also compile it to generate an executable file.

How to write comments

You can write comments (notes) in a .swift file. Comments are ignored by the compiler, so they are useful for leaving descriptions or notes about the code.

SyntaxDescription
// commentA single-line comment. Write it with one space after //. Everything from // to the end of the line is a comment.
/* comment */A multi-line comment. Everything from /* to */ is a comment. Unlike other languages, nesting is supported.
import Foundation

/*
  A nestable comment.
  /* You can nest comments like this. */
  This is a Swift-specific feature.
*/
let message = "Hello, World!"
print(message)

Swift's multi-line comments (/* */) support nesting. This is a Swift-specific feature not found in many other languages.

How to run

Run directly with the swift command

The swift command lets you run a .swift file directly like a script without compiling. It is handy for quick checks.

hello.swift
swift hello.swift
Language: Swift
Version: 5
Hello, World!

Compile with swiftc and run

The swiftc command compiles a .swift file and generates an executable, which you then run. The -o option specifies the output filename.

swiftc hello.swift -o hello
./hello

When compilation succeeds, an executable file with the specified name is generated.

Language: Swift
Version: 5
Hello, World!

Comparison of run methods

MethodCommandCharacteristics
swift (direct run)swift filename.swiftNo compilation needed; fast for quick checks. Suitable for quick verification during development.
swiftc (compile)swiftc filename.swift -o output → ./outputGenerates an executable file. Suitable for distribution or production use.

Summary

A .swift file is simply a plain text file. You can create one by writing Swift code in a text editor and saving it with a .swift extension.

Swift has two ways to run code. The swift command lets you run directly without compiling for quick checks. Use swiftc to compile when you need a distributable executable. For day-to-day development, running directly with swift is convenient for quick verification.

For recommended editors and environment setup, see Setup.

If you find any errors or copyright issues, please .