Swift is Apple's modern, type-safe programming language for iOS, macOS, watchOS, tvOS, and visionOS development, introduced in 2014 to replace Objective-C. Designed with protocol-oriented programming at its core, Swift combines the performance of compiled languages with the expressiveness of scripting languages while prioritizing safety through features like optionals, strong typing, and Swift 6's compile-time data-race safety. A key mental model: Swift encourages value types (structs) over reference types (classes) for better performance and predictability β think "copy-by-default" rather than "share-by-reference," which fundamentally shapes how you architect Swift applications.
What This Cheat Sheet Covers
This topic spans 28 focused tables and 221 indexed concepts, 154 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Type System Fundamentals
The named-type building blocks every Swift program is assembled from. The single decision that shapes most designs is the first one here β struct (a value, copied) versus class (a reference, shared) β and the protocol entry is the seed of Swift's protocol-oriented style.
| Type | Example | Description | |
|---|---|---|---|
struct Point { var x: Int var y: Int} | β’ Value type β copied on assignment or when passed β’ preferred for most data models. | ||
class Person { var name: String init(name: String) { self.name = name }} | β’ Reference type allocated on the heap β shared when assigned β’ supports inheritance and deinitializers; use when shared mutable state or identity is needed. | ||
enum Status { case success case failure(Error)} | β’ Value type for a group of related values β’ supports associated values and raw values; enables exhaustive pattern matching. | ||
protocol Drawable { func draw()} | β’ Defines a blueprint of requirements (methods, properties) that conforming types must implement β’ foundation of protocol-oriented programming. | ||
let point = (x: 10, y: 20)print(point.x) | β’ Lightweight grouping of multiple values into a single compound value β’ useful for returning multiple values from functions. | ||
typealias Coordinate = (Int, Int) | β’ Creates an alternative name for an existing type β’ improves readability without creating a new type. | ||
enum Direction: CaseIterable { case north, south, east, west}Direction.allCases.count | β’ Protocol that synthesizes allCases for enums without associated valuesβ’ lets you enumerate every case at runtime. | ||
indirect enum Tree { case leaf(Int) case node(Tree, Tree)} | β’ Lets an enum case store an instance of the same enum as an associated value β’ needed for recursive data structures like trees or linked lists; apply to the whole enum or to individual cases. |