Swift Introduction
What Swift is, Apple's platforms plus server-side Swift, installing it, and hello world.
What is Swift?
Swift is a modern, statically-typed, compiled programming language created by Apple and first released in 2014 as the intended successor to Objective-C. It was designed from the ground up to be safe (eliminating whole classes of bugs common in C-family languages), fast (compiling to native machine code via LLVM, the same backend used by Clang), and expressive (concise, readable syntax without sacrificing power).
print("Hello, Swift!")
Swift became open source in 2015, and while it's still most associated with Apple's platforms, it now runs on Linux and Windows too, and has a growing presence server-side.
The Apple ecosystem
Swift is the primary language for building apps across every Apple platform:
- iOS / iPadOS — using UIKit or the newer, declarative SwiftUI framework.
- macOS — native desktop apps via AppKit or SwiftUI.
- watchOS and tvOS — Apple Watch and Apple TV apps.
Server-side Swift, briefly
Swift isn't limited to Apple's client platforms — frameworks like Vapor and Hummingbird let you write backend web services and APIs in Swift, taking advantage of the same performance and safety guarantees on the server. This is a smaller niche compared to iOS/macOS development, but a genuinely production-viable one — you'll encounter it if you ever need a Swift codebase to share models or logic between an iOS client and its backend.
Why use Swift?
- Type safety — the compiler catches type mismatches, and Optionals (covered in depth in the next page) make the possibility of a missing value explicit and checked, instead of a silent runtime crash.
- Performance — compiles to native machine code via LLVM; Apple has published benchmarks showing Swift outperforming both Objective-C and Python on common algorithms.
- Modern syntax — closures, generics, pattern matching and protocol-oriented programming are all first-class, without needing verbose boilerplate.
- Memory safety with predictable performance — Swift uses Automatic Reference Counting (ARC) rather than a tracing garbage collector, giving deterministic deallocation without GC pauses (covered later in this track).
- Interoperability — Swift can call existing Objective-C (and, increasingly, C++) code directly, which mattered enormously for the huge existing Objective-C codebases Swift needed to coexist with.
Installing Swift
On macOS, Swift ships bundled with Xcode, available free from the Mac App Store — it includes the compiler, debugger, Interface Builder, and iOS/macOS simulators.
xcode-select --install # command line tools, if you don't need the full Xcode IDE
swift --version # confirm the toolchain is installed
On Linux or Windows, you can install the Swift toolchain directly from swift.org without Xcode. For quick experimentation on an iPad or without installing anything, Apple also offers Swift Playgrounds — an interactive app for writing and running Swift code with immediate visual feedback, popular for learning.
Your first program
mkdir HelloSwift && cd HelloSwift
swift package init --type executable
swift run
// Sources/main.swift (or Sources/HelloSwift/main.swift, depending on toolchain version)
print("Hello, Swift!")
For quick one-off scripts, you can also just run a single file directly, no project scaffolding required:
echo 'print("Hello, Swift!")' > hello.swift
swift hello.swift
Real-world example
Nearly every app on the App Store built or updated in the last several years uses Swift, at least partially — Apple's own apps (Music, Maps, News) have been progressively rewritten in Swift and SwiftUI, and major companies like Airbnb, Lyft, and Slack have shipped large portions of their iOS apps in Swift, citing its safety guarantees and more concise syntax compared to Objective-C.
Common mistakes
- Assuming Swift only runs on Apple devices — it's open source, runs on Linux and Windows, and has a real (if smaller) server-side ecosystem via Vapor.
- Treating Swift as "just Objective-C with cleaner syntax" — its type system (especially Optionals and protocol-oriented programming, covered later in this track) represents a genuinely different way of structuring code.
- Skipping past Optionals early on because
!"makes the error go away" — force-unwrapping without understanding why is one of the most common sources of crashes in real Swift apps, covered in detail in the next page.
Interview questions
Q: What was Swift designed to replace, and why? Swift was designed as Objective-C's successor for Apple platform development. Objective-C's C-based syntax was verbose and error-prone (manual memory management before ARC, a permissive type system, easy-to-miss nil-messaging bugs); Swift aimed for equivalent or better performance with a safer type system, more concise syntax, and modern language features like closures and generics as first-class citizens.
Q: Is Swift only useful for iOS/macOS development? No — while that's its dominant use case, Swift is open source, compiles natively on Linux and Windows, and has a real server-side ecosystem (Vapor, Hummingbird) for writing backend APIs, sometimes sharing model code directly with an iOS client written in the same language.