Swift
Overview
Swift is a high-performance, memory-safe systems and application programming language with a clean modern syntax and seamless access to existing C and Objective-C code and frameworks. It powers Apple-platform apps and increasingly server-side and cross-platform software on Linux and Windows. Read this when writing Swift, designing with its concurrency model, or setting up the toolchain.
Official sources
- Docs: https://www.swift.org/documentation/
- Repo: https://github.com/swiftlang/swift
- Install / download: https://www.swift.org/install/
Install / setup
The official Linux install page recommends Swiftly, the Swift toolchain installer. Verbatim (Bash):
curl -O https://download.swift.org/swiftly/linux/swiftly-$(uname -m).tar.gz && \
tar zxf swiftly-$(uname -m).tar.gz && \
./swiftly init --quiet-shell-followup && \
. "${SWIFTLY_HOME_DIR:-$HOME/.local/share/swiftly}/env.sh" && \
hash -r
Source: https://www.swift.org/install/linux/ (macOS and Windows installers: https://www.swift.org/install/)
Core concepts
- Value vs reference types —
struct/enumare value types (copied),classis a reference type; choosing correctly shapes behavior and performance. - Optionals — the type system encodes the possible absence of a value (
T?), forcing explicit handling of nil. - Protocols and protocol-oriented programming — define capabilities and provide default implementations via extensions.
- Automatic Reference Counting (ARC) — memory for class instances is managed by reference counts; watch for retain cycles.
- Error handling — typed
throws/try/catchwithResultfor explicit, recoverable error flows. - Structured concurrency —
async/await, tasks, and actors provide safe, composable asynchronous and concurrent code.
Best practices
- Prefer value types (
struct/enum) and immutability (let) by default; reach forclassonly when reference semantics are needed. - Handle optionals explicitly with
if let/guard letrather than force-unwrapping (!). - Use protocol extensions to share behavior instead of deep class hierarchies.
- Manage Swift toolchain versions with Swiftly (the official installer) for reproducible builds.
Common pitfalls
- Force-unwrapping optionals (
value!) that can be nil → crashes at runtime; use optional binding or??defaults. - Strong reference cycles between classes/closures → use
weak/unownedcaptures to let ARC reclaim memory. - Sharing mutable state across concurrent tasks → isolate it inside an
actorto avoid data races.
Examples
struct Greeter {
func greet(_ name: String?) -> String {
guard let name, !name.isEmpty else { return "Hello, world" }
return "Hello, \(name.capitalized)"
}
}
print(Greeter().greet("ada"))
Further reading
- Documentation hub: https://www.swift.org/documentation/
- Install guides per platform: https://www.swift.org/install/
Related skills
- ../cpp — the C/C++ systems languages Swift interoperates with
- ../../stacks — Swift app/server frameworks build on this language