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:
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
swiftlytoolchain manager
This course targets Swift 6 (6.0 or newer). swift --version should say Swift version 6.x.
Your first program¶
Create a project:
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:
Run it:
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?¶
printis 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 calledmain.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¶
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¶
(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 runmust be executed inside the package directory (the one containingPackage.swift). - Putting top-level code in a non-
main.swiftfile of an executable target: top-level statements are only allowed inmain.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¶
- Create a package called
greeterthat prints your name, your favourite hobby, and today's date on three separate lines (just hard-code the text for now). - Use
printwithseparator:andterminator:to print1 | 2 | 3on one line, built from three separate arguments. - Ask for the user's name and favourite number with
readLine(), then print a sentence using both via interpolation. (The number will be aStringfor now — that's fine.) - Introduce a typo on purpose (
pritn("hi")). Read the error message. Fix it. - Run
swift build -c releaseand 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 — nomain()needed. printwrites to the console;readLine()reads a line (as an optional).\(expression)interpolates values into strings.