Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

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.

  • Beginner
  • 10 minutes
  • Examples run with Swift 6.4 and Python 3.14.8
  • By MySmartCoPilot
Free: 10 minutes

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:

Interactive · stage by stage One Swift source, six kinds of platform Step through the build. The first two boxes stay the same; watch the SDK and the program change for each platform.
One Swift source, six kinds of platform, step 9 of 9. Showing Your .swift files, Swift compiler, SDK for the target (one per platform), The program (one per platform).Your .swift filesthe same sourceSwift compilerswiftcSDK for the targetone per platformThe programone per platform

The drawing shows the whole scene; its 9 steps are listed below.

Every step as text
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Windows. On Windows the toolchain, installed together with Visual Studio's build tools, builds Windows programs.
  6. 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.
  7. WebAssembly. The Swift SDK for WebAssembly compiles to Wasm modules, which run in web browsers and in other WebAssembly runtimes.
  8. 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.
  9. 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

  1. Question 1 of 2 You want Swift code inside an Android app. Where do you run the Swift compiler?

    Choose one answer.

    A hint

    Think about where the official SDK for Android is installed.

    Show the answer: Question 1 of 2

    Answer: On a Mac or a Linux computer, with the Swift SDK for Android

    Android and WebAssembly are built by cross-compiling: the compiler runs on a Mac or a Linux computer with the SDK for the target, and the result moves to the phone or the web page.

  2. Question 2 of 2 What is Embedded Swift?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: A compilation mode that produces small, freestanding programs for microcontrollers

    Embedded Swift is a compilation mode for microcontrollers. It gives up the features that need the Swift runtime and produces small programs that run with no operating system around them. Swift.org still calls it experimental.

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: what was this built for? Swift · platform_report.swift
// 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:

Build the report, then run it Python · platform_session.py
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

  1. Question 1 of 2 When is the condition of #if os(Linux) decided?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: When the file is compiled, for the platform the build is for

    #if conditions are evaluated at compile time, for the platform the program is being built for. The branch whose condition is false is not compiled, so the program that comes out contains only the code for that platform.

  2. Question 2 of 2 This file is built and run on a Linux server. What does it print?

    Read the code, then choose one answer.

    #if canImport(SwiftUI)
    print("SwiftUI is here")
    #else
    print("no SwiftUI")
    #endif
    Show the answer: Question 2 of 2

    Answer: no SwiftUI

    canImport(SwiftUI) asks whether the module can be imported, without importing it. On Linux the answer is no, the compiler keeps the #else branch, and the program prints "no SwiftUI".

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:

Timeline · 8 events in order Swift releases that changed what you can build Walk through the releases in order. Each event says what that release added.
  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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. 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.

  7. 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.

  8. 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, and swift --version names 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

  1. Question 1 of 2 Your Mac has Xcode 26.6, which contains Swift 6.3. A package you downloaded uses @diagnose, an attribute new in Swift 6.4. What happens when you build it?

    Choose one answer.

    A hint

    Which compiler does Xcode 26.6 run?

    Show the answer: Question 1 of 2

    Answer: The build fails, because the Swift 6.3 compiler does not know the attribute; a Swift 6.4 compiler is needed

    The compiler comes with your tools: Xcode 26.6 runs Swift 6.3, and Xcode 27 runs Swift 6.4 (it needs macOS Tahoe 26.6 or later). A package that uses a 6.4 feature builds only with a 6.4 compiler, whatever the language mode.

  2. Question 2 of 2 Which statement about Swift 6.4 is right?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: The Swift 6.4 compiler can build code in the Swift 5 language mode

    A version such as 6.4 names a compiler. A language mode such as Swift 6 names a set of rules, and the 6.4 compiler can apply the Swift 6, 5, 4.2 or 4 rules.

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

  1. Question 1 of 2 Which of these runs only on macOS?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: Xcode

    Xcode is the one macOS-only item here. The package manager, Foundation and Swift Testing all work on Linux and Windows as well.

  2. Question 2 of 2 You write a command-line tool for a Linux server. Which of these can it use? Choose every one that fits.

    Choose every answer that is right.

    Show the answer: Question 2 of 2

    Answer:

    • The Swift Package Manager
    • The standard library (Int, String, Array, print)
    • Foundation

    The open-source parts of Swift, the language with its standard library, Foundation, the package manager and Swift Testing, work on Linux. SwiftUI, UIKit and Xcode belong to Apple's platforms.

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:

typo.swift: a misspelled name Swift · typo.swift
let fare = 40
pritn("Fare: \(fare) rupees")
A build that fails Python · typo_session.py
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:

session.py: plays a session and prints it Python · session.py
"""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

  1. One question In a session of this track, which lines were typed as commands?

    Choose one answer.

    Show the answer: One question

    Answer: The lines that start with $

    A session reads like a terminal: "$ " and a command, then what that command printed, errors included. The first line names the compiler, and a failed command ends with its exit status.

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.

  1. Question 1 of 10 Which SDK do you use to build Swift code that runs in a web browser?

    Choose one answer.

    Show the answer: Question 1 of 10

    Answer: The Swift SDK for WebAssembly

    Each kind of platform has its own SDK. For a browser it is the Swift SDK for WebAssembly, which turns Swift into Wasm modules.

  2. Question 2 of 10 A command-line tool must run on Linux machines where nothing may be installed. What does Swift offer for that?

    Choose one answer.

    Show the answer: Question 2 of 10

    Answer: The Static Linux SDK, which builds fully static executables with no external dependencies

    Since Swift 6, the Static Linux SDK builds fully statically linked Linux executables. They run without Swift or any other library installed, and the SDK can build them on another operating system.

  3. Question 3 of 10 Which line starts a block of code that is compiled only in Windows builds?

    Choose one answer.

    Show the answer: Question 3 of 10

    Answer: #if os(Windows)

    Platform conditions such as os(Windows), arch(arm64), compiler(>=6.4) and canImport(SwiftUI) go after #if or #elseif, and the block ends with #endif.

  4. Question 4 of 10 This file is compiled by the Swift 6.4 compiler in the Swift 5 language mode. What does it print?

    Read the code, then choose one answer.

    #if compiler(>=6.4)
    print("A")
    #else
    print("B")
    #endif
    A hint

    compiler() and swift() ask about different versions.

    Show the answer: Question 4 of 10

    Answer: A

    The Swift Programming Language says that the version for compiler() is the compiler version, regardless of the language mode. To ask about the language mode, use swift(): here #if swift(>=6) would be false.

  5. Question 5 of 10 Which language modes does the Swift 6.4 compiler of Xcode 27 offer?

    Choose one answer.

    Show the answer: Question 5 of 10

    Answer: Swift 6, 5, 4.2 and 4

    A compiler supports several language modes. Xcode 27, with the Swift 6.4 compiler, offers the Swift 6, 5, 4.2 and 4 modes.

  6. Question 6 of 10 A teammate wants Xcode 27 to get Swift 6.4, but their Mac runs macOS Sequoia 15.6. What do you tell them?

    Choose one answer.

    Show the answer: Question 6 of 10

    Answer: Xcode 27 needs macOS Tahoe 26.6 or later, so macOS has to be updated first

    The compiler you have depends on your tools, and your tools depend on your system: Xcode 27 contains Swift 6.4 and needs macOS Tahoe 26.6 or later.

  7. Question 7 of 10 On a Linux machine, where does Foundation come from?

    Choose one answer.

    Show the answer: Question 7 of 10

    Answer: From the Swift toolchain, which carries swift-foundation

    swift-foundation is a shared implementation of Foundation's core API, written in Swift. On Apple platforms it is inside Apple's Foundation framework; elsewhere it comes with the toolchain.

  8. Question 8 of 10 A tutorial says "change this in Xcode's build settings". You build a package with swift build on Linux. What do you do?

    Choose one answer.

    Show the answer: Question 8 of 10

    Answer: Look for the same setting for packages, in Package.swift or as an option of swift build

    The advice was written for another layer, an Xcode project. For a package, the same thing is usually a setting in Package.swift or a flag for swift build, which later lessons of this track show.

  9. Question 9 of 10 A session starts with # Apple Swift version 6.4 (swiftlang-6.4.0.34.1). What does that line tell you?

    Choose one answer.

    Show the answer: Question 9 of 10

    Answer: Which Swift compiler ran every command of the session

    The first line names the compiler. Your own output can differ in details when your compiler or your system is a different one.

  10. Question 10 of 10 A session shows $ swiftc broken.swift -o app, an error and [exit status 1], then $ ./app followed by an error and [exit status 127]. What happened?

    Choose one answer.

    A hint

    What does a failed build leave behind?

    Show the answer: Question 10 of 10

    Answer: The build failed, so no app file was written, and the shell could not find ./app to run

    Read a session top to bottom: the first failure explains the second. The compiler stopped at an error, so the file app was never written, and running it failed with status 127, the shell's code for a command it cannot find.

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.
  • #if conditions such as os(), arch(), compiler() and canImport() 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

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.