Skip to content

1. Setup & your first program

What & why

Swift is a compiled, type-safe language from Apple, now open source and available on macOS, Linux, and Windows. You'll write Swift for apps, command-line tools, and servers. In this course we use the command line and Swift Package Manager (SwiftPM) so everything works the same on every platform — no Xcode required (though Xcode works fine too).

Install the toolchain

Check whether you already have it:

swift --version

If not:

  • macOS: install Xcode from the App Store, or just the command-line tools: xcode-select --install
  • Windows: winget install --id Swift.Toolchain -e (you'll also need Visual Studio Build Tools with the C++ workload — the installer guides you)
  • Linux: follow the instructions at swift.org/install or use the swiftly toolchain manager

This course targets Swift 6 (6.0 or newer). swift --version should say Swift version 6.x.

Your first program

Create a project:

mkdir hello && cd hello
swift package init --type executable --name hello

That generates a package with a Package.swift manifest and a Sources/ folder. Open Sources/main.swift (or Sources/hello/main.swift, depending on your toolchain version) and replace its contents:

print("Hello, Swift!")

Run it:

swift run

You'll see Hello, Swift!. swift run compiles (the first build is slower) and then runs the result.

Quick experiments

For a single file you can skip the package: save code as scratch.swift and run swift scratch.swift. This is great for the short examples in this course.

What just happened?

print("Hello, Swift!")
  • print is a function from the standard library. It writes its arguments to standard output followed by a newline.
  • There is no main() function and no semicolons. In a file called main.swift, top-level code runs top to bottom — that is the program's entry point.
  • "Hello, Swift!" is a string literal.

Printing several things

print("Score:", 42)                     // Score: 42   (separator is a space)
print("a", "b", "c", separator: "-")    // a-b-c
print("no newline", terminator: "")     // stays on the same line
print()                                 // just a blank line

Comments

// A single-line comment

/* A multi-line comment
   (these can /* nest */ safely) */

/// A documentation comment — shows up in Xcode's Quick Help and DocC.

Reading input

print("What's your name?")
if let name = readLine() {
    print("Nice to meet you, \(name)!")
}

readLine() returns String? — an optional, because there might be no input (end of file). Don't worry about if let yet; Optionals explains it. Run this with swift run and type a name.

\(name) is string interpolation: it puts the value of any expression inside a string.

The build commands you'll use

swift build              # compile only (debug build)
swift build -c release   # optimized build
swift run                # build + run
swift run hello arg1     # pass command-line arguments
swift test               # run tests (later lesson)

Compiler errors tell you the file, line, and problem. Read the first error first; later ones are often fallout.

Playing in the REPL

swift repl

(or swift on some setups) opens an interactive prompt where you can type expressions and see results immediately. Handy for quick questions like "what does this return?".

Common mistakes

  • Running from the wrong folder: swift run must be executed inside the package directory (the one containing Package.swift).
  • Putting top-level code in a non-main.swift file of an executable target: top-level statements are only allowed in main.swift (or when using @main, see later lessons).
  • Smart quotes: copying code from a word processor can turn " into “. Swift will reject it.
  • Expecting semicolons to be required: they're optional — only needed to put two statements on one line.

Exercises

  1. Create a package called greeter that prints your name, your favourite hobby, and today's date on three separate lines (just hard-code the text for now).
  2. Use print with separator: and terminator: to print 1 | 2 | 3 on one line, built from three separate arguments.
  3. Ask for the user's name and favourite number with readLine(), then print a sentence using both via interpolation. (The number will be a String for now — that's fine.)
  4. Introduce a typo on purpose (pritn("hi")). Read the error message. Fix it.
  5. Run swift build -c release and find the executable under .build/release/. Run it directly.

Recap

  • Swift runs on macOS, Linux, and Windows; SwiftPM (swift package init, swift run) is the common workflow.
  • In main.swift, top-level statements are the program — no main() needed.
  • print writes to the console; readLine() reads a line (as an optional).
  • \(expression) interpolates values into strings.

Next: Constants, variables & types →