# Swift Language Reference
## Languages — The Codex Coding
**URL:** https://thecodex.expert/coding/languages/swift/
**Type:** language-reference | **Confidence:** high | **Last verified:** 2026-06-01

## CANONICAL DEFINITION
Swift is a statically typed, compiled, multi-paradigm programming language created by Apple that replaces Objective-C for iOS, macOS, watchOS, and visionOS development — providing optionals for null safety, value semantics, protocol-oriented programming, async/await with actors, and ARC memory management with no garbage collector.

## QUICK FACTS
- **Created:** June 2014 (WWDC); open-sourced December 2015 by Chris Lattner (Apple)
- **Current:** Swift 5.10 / 6.0 (2024)
- **Typing:** Static, type-inferred
- **Memory:** ARC (Automatic Reference Counting) — NOT garbage collection
- **Platforms:** iOS, macOS, watchOS, tvOS, visionOS, Linux, Windows
- **Replaces:** Objective-C; 100% interoperable via bridging header

## CRITICAL NOTES
- **struct = value type** (copied on assignment) | **class = reference type** (shared on assignment)
- **! force-unwrap CRASHES** if nil — "Unexpectedly found nil while unwrapping an Optional value"; use if let / guard let / ??
- **ARC is NOT garbage collection** — retain/release calls inserted at compile time; deterministic deallocation at scope end
- **Reference cycles** prevent deallocation; break with `weak` (auto-nil on dealloc) or `unowned` (crash if accessed after)
- **Swift + Objective-C coexist** — bridging header; no rewrite needed for legacy code
- **let = constant; var = variable** (same semantics as Kotlin's val/var)

## OPTIONALS
- `T` = non-optional (never nil) | `T?` = Optional (nil or value)
- `if let x = optional { }` — safe bind; runs only if non-nil
- `guard let x = optional else { return }` — early exit if nil
- `optional ?? defaultValue` — nil coalescing
- `optional?.property` — safe call; returns nil if optional is nil
- `optional!` — FORCE UNWRAP; crashes if nil (use only with absolute certainty)
- `optional.map { transform($0) }` — functional transform

## VALUE VS REFERENCE
- **struct**: copied on assignment; mutations to copy don't affect original
- **class**: reference copied; mutations through any reference affect all
- Swift stdlib types (String, Array, Dictionary, Int) are ALL structs
- Copy-on-Write (CoW): actual copying deferred until mutation — most copies are O(1)
- Prefer struct; use class when identity/shared mutation is needed

## MEMORY MANAGEMENT (ARC)
- Strong references (default): increment ref count
- `weak var ref: MyClass?` — doesn't increment count; auto-nils when object deallocated
- `unowned let ref: MyClass` — doesn't increment count; crashes if accessed after dealloc
- Closure capture: `[weak self]` or `[unowned self]` to avoid retain cycles

## CONCURRENCY (Swift 5.5+, 2021)
- `async func fetchData() async throws -> Data` — suspendable function
- `await expression` — suspend until result available
- `async let a = fn1(); async let b = fn2(); (a, b) = await (a, b)` — parallel
- `Task { }` — unstructured concurrency | `TaskGroup` — dynamic task fan-out
- `actor` — serialises access to mutable state; compile-time actor isolation checking
- `@MainActor` — code must run on main thread (UI updates)

## KEY VERSIONS
- Swift 2.0 (2015): error handling (try/catch/throw)
- Swift 3.0 (2016): API rename, open-sourced
- Swift 5.0 (2019): ABI stability — runtime in OS
- Swift 5.5 (2021): async/await, actors, structured concurrency
- Swift 5.7 (2022): opaque return types (some), any existentials
- Swift 5.9 (2023): macros (compile-time code generation)
- Swift 6.0 (2024): complete concurrency checking by default

## SWIFTUI
- Introduced 2019; declarative UI; View conforming structs
- `@State` — local view state; `@StateObject` — reference type lifecycle
- `@Binding` — two-way binding to parent state
- `@EnvironmentObject` — injected dependency
- `@ObservedObject` — observe external observable object
- `some View` — opaque return type; compiler knows concrete type

## SOURCES
- The Swift Programming Language: docs.swift.org/swift-book/
- Swift API Design Guidelines: swift.org/documentation/api-design-guidelines/
- Swift Evolution: github.com/apple/swift-evolution

*The Codex Coding — thecodex.expert/coding/ — Free forever — Mumbai, India*
