Swift Module 1 – Getting started: Swift 6.4, toolchains and your first program
What Swift is and where it runs
What the Swift language is, the six kinds of platform its programs run on, how its releases arrive, and how Swift differs from Xcode, SwiftUI and Foundation.
What you will learn
- Describe the six kinds of platform Swift programs run on, from Apple devices and Linux servers to microcontrollers
- Explain Swift's release rhythm and tell a compiler version such as Swift 6.4 from a language mode such as Swift 6
- Distinguish the Swift language from Xcode, SwiftUI, Foundation and the Swift Package Manager
- Use #if os(), arch(), compiler() and canImport() to report what a program was built for
- Read a terminal session of this track and say which lines were typed and which were printed
What you’ll learn
What you will learn
- Describe the six kinds of platform Swift programs run on, from Apple devices and Linux servers to microcontrollers
- Explain Swift's release rhythm and tell a compiler version such as Swift 6.4 from a language mode such as Swift 6
- Distinguish the Swift language from Xcode, SwiftUI, Foundation and the Swift Package Manager
- Use #if os(), arch(), compiler() and canImport() to report what a program was built for
- Read a terminal session of this track and say which lines were typed and which were printed
On this page
Swift is best known as the language of iPhone and Mac apps, yet Apple platforms are only one of six kinds of system its compiler builds programs for. The same language also gives you servers and command-line tools for Linux, programs for Windows, code for Android apps, WebAssembly modules for web browsers and small programs for microcontrollers.
What you can use depends on that target: the language is the same everywhere, while some libraries exist only on some platforms. This lesson separates the language from the tools and frameworks around it, builds one file that reports what it was compiled for, and shows how Swift releases arrive and which one this track uses.
Swift programs run on six kinds of platform
The overview of the language on swift.org lists six kinds of platform: Apple’s devices, Linux, Windows, Android, WebAssembly and microcontrollers. Each needs its own SDK: A software development kit: the libraries and settings a compiler needs to build programs for one target, such as Apple's macOS SDK, the Static Linux SDK or the Swift SDK for Android., the libraries and settings for that target, but the language and its compiler stay the same. Step through one set of files on its way to each of them, and watch the third and fourth boxes change while the first two do not:
The drawing shows the whole scene; its 9 steps are listed below.
Every step as text
- One source. Everything starts as .swift files. The language is the same on every platform, with the same syntax, types and rules, so the same file can be built for several of them.
- The compiler. The compiler reads the files, checks their types and the rules of the language mode, and translates them for one target at a time.
- Apple platforms. For iOS, iPadOS, macOS, watchOS, tvOS and visionOS, the libraries come from Apple's SDKs, which also hold Apple-only frameworks such as SwiftUI. Xcode installs them on a Mac; the Command Line Tools carry the macOS SDK alone.
- Linux. On Linux the toolchain builds servers and command-line tools. The Static Linux SDK builds fully static Linux executables, which run on a Linux machine with nothing else installed, even when built on a Mac.
- Windows. On Windows the toolchain, installed together with Visual Studio's build tools, builds Windows programs.
- Android. The official Swift SDK for Android cross-compiles on a Mac or a Linux computer, for native Android programs written in Swift or for Swift code inside Android apps written in Kotlin or Java.
- WebAssembly. The Swift SDK for WebAssembly compiles to Wasm modules, which run in web browsers and in other WebAssembly runtimes.
- Microcontrollers. Embedded Swift is a compilation mode that restricts some language features so that programs need no Swift runtime, and produces small, freestanding programs for microcontrollers. Swift.org still calls it experimental.
- Same language, other libraries. Six kinds of target, one language. What changes is the SDK, the set of libraries that exist on the target, so code that needs a library must first ask whether it is there.
Often the target is not the computer you build on. Android and WebAssembly programs are built by cross-compiling: you build on a Mac or a Linux computer with the SDK for the target, then move the result to the phone or the web page, just as an iPhone app is built on a Mac. Embedded Swift is not an operating system at all but a compilation mode: it restricts some language features so that a program needs no Swift runtime, which is what lets it fit on a small chip.
The standard library: The library that comes with the Swift language on every target, with types such as Int, String and Array and functions such as print. Code uses it without any import., with Int, String, Array and print, is part of the
language and is there on every target, in a smaller form for Embedded Swift. Everything above it, from Apple’s interface
frameworks to the libraries of a Linux server, depends on where the program is going.
Checkpoint
Check: Where Swift programs run
One file can report which platform built it
A program meant for several platforms has to know, while it is being compiled, what the target offers. Swift’s
conditional compilation: Code between #if and #endif that the compiler keeps or leaves out when it builds, depending on conditions such as os(Linux), arch(arm64), compiler(>=6.4) or canImport(SwiftUI). does that: code between #if and #endif is compiled
only when its condition is true for the target, the platform the build is for. This file asks four such questions, about
the operating system, the processor, the compiler and whether a module can be imported:
// platform_report.swift: one source file that reports what it was built for.
// Each #if is decided by the compiler for the platform it builds for:
// only the branch whose condition is true for that target is compiled.
#if os(macOS)
let system = "macOS"
#elseif os(iOS)
let system = "iOS"
#elseif os(Linux)
let system = "Linux"
#elseif os(Windows)
let system = "Windows"
#else
let system = "another platform"
#endif
#if arch(arm64)
let processor = "arm64"
#elseif arch(x86_64)
let processor = "x86_64"
#elseif arch(wasm32)
let processor = "wasm32 (WebAssembly)"
#else
let processor = "another processor"
#endif
#if compiler(>=6.4)
let compiler = "Swift 6.4 or newer"
#else
let compiler = "older than Swift 6.4"
#endif
#if canImport(SwiftUI)
let swiftUI = "can be imported (this SDK has it)"
#else
let swiftUI = "cannot be imported (not in this SDK)"
#endif
print("Built for: \(system) on \(processor)")
print("Compiler: \(compiler)")
print("SwiftUI: \(swiftUI)") Built with the Swift 6.4 compiler on a Mac with Apple silicon and then run, it prints:
from session import run, version
# Build the report with the Swift 6 language mode, then run the program the compiler wrote.
version()
run("""
swiftc -language-mode 6 platform_report.swift -o platform_report
./platform_report
""") Output
# Apple Swift version 6.4 (swiftlang-6.4.0.34.1) $ swiftc -language-mode 6 platform_report.swift -o platform_report $ ./platform_report Built for: macOS on arm64 Compiler: Swift 6.4 or newer SwiftUI: can be imported (this SDK has it)
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 platform_session.py
Each line comes from the one branch the compiler kept. os(macOS) and arch(arm64) were true because the build was
for this Mac; built on the same Mac for WebAssembly, both would be false and the wasm32 branch would be kept.
compiler(>=6.4) compares the version of the compiler, whatever language mode: The set of language rules the compiler applies to your code: Swift 6, 5, 4.2 or 4. One compiler supports several modes, chosen with -language-mode or a package setting; the Swift 6 mode reports possible data races as errors. More in “Compiler errors, language modes and feature flags” it was
asked to use, which the compiler-diagnostics lesson of this module returns to. canImport(SwiftUI) was true because the
macOS SDK contains SwiftUI; it only asks whether the module exists and never imports it. Built on Linux, the same file
keeps the Linux branch instead and reports that SwiftUI cannot be imported, because SwiftUI is an Apple framework for
Apple’s own platforms only.
Checking the platform with an ordinary if
An if is decided when the program runs, so both of its branches must compile on every platform. Code that uses
SwiftUI inside if isMac { … } still fails to build on Linux, where there is no SwiftUI to compile against. Put it
inside #if canImport(SwiftUI) … #endif, which the Linux build never compiles.
Checkpoint
Check: Compile-time platform checks
A new Swift arrives about every six months
Swift 6.2, 6.3 and 6.4 came out about six months apart, and each added to what you can build. Walk through the releases that changed the most, from the first announcement to the version this track uses:
- Introduced
Swift is announced
Apple introduced Swift in 2014 as a new language for iOS and OS X apps, designed to work beside existing Objective-C code.
- Open source
Swift becomes an open project open source
In 2015 the language, its libraries, the debugger and the package manager were released under the Apache 2.0 licence with a runtime library exception, and Swift.org was created to host the project.
- 5.0
A stable ABI on Apple platforms
In 2019 Swift 5 declared its ABI stable on Apple platforms. The Swift runtime and libraries now ship with Apple's operating systems, so apps no longer carry their own copy and become smaller.
- 5.5
async/await and actors concurrency
In 2021 Swift 5.5 added concurrency to the language itself, with async and await, structured concurrency and actors.
- 6.0
The Swift 6 language mode data-race safety
In 2024 Swift 6 added an opt-in language mode that reports possible data races as compile errors, the Swift Testing library, one Swift implementation of Foundation for every platform and fully static Linux executables.
- 6.2
WebAssembly and easier concurrency
Swift 6.2 added support for WebAssembly, an option to run code on the main actor by default, and settings in Package.swift that control warnings.
- 6.3
An official SDK for Android
About six months after 6.2, Swift 6.3 brought the first official release of the Swift SDK for Android and the @c attribute, which exposes Swift functions to C code.
- 6.4
The version this track uses this track
Six months later, Swift 6.4 made Swift Build the default build system of the Swift Package Manager, added the @diagnose attribute for controlling warnings in source code, brought Subprocess 1.0 and opened a documentation site on docs.swift.org.
The timeline has 8 events, from Introduced to 6.4.
Three facts about versions explain most differences between your results and this track’s:
- A version number names a compiler, not a set of rules. Swift 6.4 is a compiler; the Swift 6 language mode is one set of rules it can apply. One compiler accepts several modes: Apple’s table for Xcode 27, which contains Swift 6.4, lists the Swift 6, 5, 4.2 and 4 modes.
- Your tools decide which compiler you have. Each Xcode release ships one Swift compiler: Xcode 26.5 and 26.6 contain Swift 6.3, while Xcode 27 contains Swift 6.4 and needs macOS Tahoe 26.6 or later. Code that uses something new in 6.4 does not build with the compiler of an older Xcode.
- Apple and swift.org name their builds differently. The swift.org install page offers 6.4.1, a patch release of
6.4, while Apple’s compiler in the Command Line Tools that ran this track’s examples reports
Apple Swift version 6.4 (swiftlang-6.4.0.34.1). Both are Swift 6.4 compilers, andswift --versionnames the exact build you have.
This track uses Apple’s Swift 6.4 compiler, from the Command Line Tools on a Mac with Apple silicon, and compiles single
files in the Swift 6 language mode with -language-mode 6. A lesson that needs something else, such as another
operating system, says so where it starts.
Key idea
A version such as Swift 6.4 names a compiler, a language mode such as Swift 6 names the rules it applies, and the Xcode or toolchain you install decides which compiler you have.
Checkpoint
Check: Releases, compilers and language modes
Swift, Xcode, SwiftUI and Foundation are different things
Questions about Swift often mix up layers that come from different places, and an answer about one layer rarely applies to another. The table puts each name in its layer:
| Name | What it is | Where it works |
|---|---|---|
| Swift | The language, the compiler swiftc and the standard library |
All six kinds of platform |
| Swift Package Manager | The tool that builds, runs and tests packages | Wherever a toolchain is installed |
| Foundation | A library for dates, files, URLs, JSON and more | Apple platforms, Linux, Windows |
| Swift Testing | The library for writing tests | Apple platforms, Linux, Windows |
| Xcode | Apple’s app for building apps, with editor and debugger | macOS only |
| SwiftUI, UIKit | Apple’s frameworks for user interfaces | Apple platforms only |
The first four belong to the open-source Swift project, whose language, libraries, debugger and package manager are released under the Apache 2.0 licence with a runtime library exception. On Apple platforms, Foundation: A library of common building blocks beyond the standard library, such as dates, files, URLs and JSON. Apple platforms have Apple's Foundation framework; on Linux and Windows the toolchain carries swift-foundation. is Apple’s own framework; on the other platforms the toolchain carries swift-foundation, a shared implementation of the same core API written in Swift. Xcode, SwiftUI and UIKit are Apple’s products: Xcode runs only on macOS, and Apple documents SwiftUI for iOS, iPadOS, macOS, tvOS, visionOS and watchOS only.
So a command-line tool for a Linux server can use the standard library, Foundation, the package manager and Swift Testing, and none of the last two rows. This track teaches the first four rows; user interfaces and app projects in Xcode build on them and come later.
Name the layer when you search
Add the layer to a search: “Swift language”, “SwiftPM”, “Foundation” or “SwiftUI”. An answer that starts with
“in Xcode’s build settings” does not help with a package you build with swift build.
Checkpoint
Check: Language, tools and frameworks
Reading a session: commands, then what they printed
These pages cannot run Swift code in your browser, so every program in this track is shown the way it ran in a terminal:
as a session. A line that starts with $ is a command that was typed, and the lines under it are everything the command
printed, errors included. The first line, which starts with #, names the compiler that ran the session, and a command
that fails ends with its exit status. Here the compiler stops at a misspelled function name:
let fare = 40
pritn("Fare: \(fare) rupees") from session import run, version
# A command that fails: the line starts with "!", so the session expects it to.
version()
run("""
!swiftc -language-mode 6 typo.swift -o typo
""") Output
# Apple Swift version 6.4 (swiftlang-6.4.0.34.1)
$ swiftc -language-mode 6 typo.swift -o typo
typo.swift:2:1: error: cannot find 'pritn' in scope
1 | let fare = 40
2 | pritn("Fare: \(fare) rupees")
| `- error: cannot find 'pritn' in scope
3 |
[exit status 1]
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 typo_session.py
The error starts with the place, typo.swift:2:1 for line 2, column 1, then shows that line with a marker under the
name it could not find, and the [exit status 1] line says the command failed. Reading such messages closely is the
subject of the last lesson of this module.
Each session is a short Python script that types the commands for you and prints what they print. You can type the same commands yourself, in a terminal opened in a folder where you have saved the lesson’s files, and compare:
"""Plays a terminal session and prints it as a terminal shows it: "$ " and the command, then what it printed.
Each line of a session is one command, run in this folder by /bin/sh. Standard output and standard error are printed
together, in the order the program wrote them, and a command that fails is followed by "[exit status N]". A line that
starts with "!" is a command that is meant to fail, such as a file that does not compile; it is printed without the
"!". When a command ends differently from what its line says, this script exits with status 1, so a session that went
wrong is never kept as if it had worked. A line "cd <folder>" moves the session into that folder, as in a terminal.
version() prints the first line of `swift --version` as a comment, so every session says which compiler ran it, and
stops the session when that compiler is not the one the lesson was written for. To repeat a session on your own
computer, open a terminal in the folder that holds the lesson's files and type the commands that follow "$ ".
"""
import os
import re
import subprocess
import sys
SWIFT_VERSION = re.compile(r"Apple Swift version (\S+) \((swiftlang-[^ )]+)")
CD = re.compile(r"cd (\S+)")
def _execute(command):
done = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
# A program stopped by a signal is reported as 128 + the signal number, as a shell reports it.
status = done.returncode if done.returncode >= 0 else 128 - done.returncode
return status, done.stdout
def version(want="6.4"):
"""Prints which Swift compiler runs the session, and stops if it is not Swift `want`."""
status, output = _execute("swift --version")
found = SWIFT_VERSION.search(output)
if status != 0 or not found or found.group(1) != want:
sys.exit(f"this session needs Swift {want}; swift --version printed: {output.strip()}")
print(f"# Apple Swift version {found.group(1)} ({found.group(2)})")
def run(script, max_lines=60):
"""Runs each line of `script` as one command and prints the session."""
unexpected = 0
for line in script.strip().splitlines():
line = line.strip()
if not line:
continue
must_fail = line.startswith("!")
command = line[1:].strip() if must_fail else line
print(f"$ {command}")
folder = CD.fullmatch(command)
if folder:
os.chdir(folder.group(1))
continue
status, output = _execute(command)
lines = output.splitlines()
for text in lines[:max_lines]:
print(text)
if len(lines) > max_lines:
print(f"[{len(lines) - max_lines} more lines]")
if status != 0:
print(f"[exit status {status}]")
if (status != 0) != must_fail:
unexpected += 1
if unexpected:
sys.exit(1) Checkpoint
Check: Reading a session
Practise
Practice here adapts to you: each question is picked from the idea you have shown least so far, and one you miss comes
back at the end. Once Swift is installed, which the next lesson does, build platform_report.swift yourself and compare
your output with the session above: on another processor or operating system, the first line changes.
Practice that adapts to you
Practise this lesson
10 questions about 5 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.
Interview questions
Warm-up: is Swift only for Apple platforms?
No. Swift is an open-source language whose compiler builds programs for Apple platforms, Linux, Windows, Android
(through the official Swift SDK for Android), WebAssembly and, in the experimental Embedded Swift mode,
microcontrollers. What is Apple-only is a set of frameworks, chiefly SwiftUI and UIKit, and Xcode, which runs only on
macOS. So a server, a command-line tool or a shared library of business logic can be written in Swift and built on Linux
or Windows, while an iPhone app needs a Mac with Xcode. A strong answer also says how code copes with the difference:
#if canImport(SwiftUI) or #if os(Linux) keeps platform-only code out of the builds where it cannot compile.
Key takeaways
- Swift programs run on six kinds of platform: Apple’s devices, Linux, Windows, Android, WebAssembly and microcontrollers; the language stays the same and the SDK, the set of libraries, changes.
#ifconditions such asos(),arch(),compiler()andcanImport()are decided when the code is compiled, so code for one platform never has to compile on another.- Swift releases arrive about every six months, a compiler version such as Swift 6.4 is not a language mode such as Swift 6, and your Xcode or toolchain decides which compiler you have.
- Swift, the Swift Package Manager, Foundation and Swift Testing work beyond Apple platforms, while Xcode, SwiftUI and UIKit stay on Apple’s.
- Every example in this track is a session: the compiler that ran it, the commands after
$, then exactly what they printed.
Terms in this lesson
- conditional compilation
- Code between #if and #endif that the compiler keeps or leaves out when it builds, depending on conditions such as os(Linux), arch(arm64), compiler(>=6.4) or canImport(SwiftUI).
- Foundation
- A library of common building blocks beyond the standard library, such as dates, files, URLs and JSON. Apple platforms have Apple's Foundation framework; on Linux and Windows the toolchain carries swift-foundation.
- SDK
- A software development kit: the libraries and settings a compiler needs to build programs for one target, such as Apple's macOS SDK, the Static Linux SDK or the Swift SDK for Android.
- standard library
- The library that comes with the Swift language on every target, with types such as Int, String and Array and functions such as print. Code uses it without any import.
References
- About Swift (Swift.org)
- Swift 6.4 Released (Swift.org)
- Swift 6.3 Released (Swift.org)
- Swift 6.2 Released (Swift.org)
- Announcing Swift 6 (Swift.org)
- Swift 5.5 Released (Swift.org)
- Swift 5 Released (Swift.org)
- Apple Releases iOS 8 SDK With Over 4,000 New APIs (the announcement of Swift) (Apple)
- The Swift Programming Language (6.4): Statements, Conditional Compilation Block (Swift.org)
- Embedded Swift language restrictions (EmbeddedRestrictions) (Swift.org)
- Install Swift on macOS (Swift.org)
- Xcode support: minimum requirements and supported SDKs (Apple)
- Installing the command-line tools (Apple)
- SwiftUI (Apple Developer Documentation) (Apple)
- swift-foundation (The Swift Project (swiftlang))
Report a problem with this lesson
Kept only in this browser. Your Learn progress