The Swift language at a glance
Swift is Apple’s modern, type-safe language for iOS, macOS, watchOS, tvOS, visionOS and server-side development. Designed for speed and safety, it offers expressive syntax, automatic memory management, and full interoperability with Objective-C. Swift 6.1 (released March 2025) is the current stable version.
async/await
and actors.How a Swift project is organised
main.swift
file in command-line tools
executes top-level code sequentially.SwiftUI
apps).
Each .swift
file belongs to exactly one module
(target) and may contain multiple declarations in any order. The compiler
performs whole-module type checking by default.
// importing Apple frameworks
import Foundation
import SwiftUI
// importing a local module
import MyUtilities
Package.swift
manifest uses
Package() DSL to declare
products, targets and dependencies.Note: Xcode wraps SPM seamlessly—every Xcode project is also a Swift package under the hood.
Declarations, literals and operators
let maximumLoginAttempts = 3 // constant
var currentAttempt = 0 // mutable variable
var message: String = "Hello"
var pi = 3.14159 // Double inferred
+ − * / %
..HalfOpen
,
...Closed
Conditionals, pattern matching and loops
if age >= 18 {
print("Adult")
} else if age > 12 {
print("Teen")
} else {
print("Child")
}
switch
switch coordinates {
case (0, 0):
print("Origin")
case (let x, 0):
print("On x-axis at \(x)")
case (-2...2, -2...2):
print("Near")
default:
print("Far")
}
for-in
over arrays, dictionaries, ranges.while
& repeat-while
.Parameter syntax, generics and higher-order code
func greet(_ name: String, from city: String = "") -> String {
"Hello \(name)!" + (city.isEmpty ? "" : " From \(city).")
}
func swapTwoValues<T>(_ a: inout T, _ b: inout T) { … }
func mean<T: BinaryInteger>(_ numbers: repeat T) -> Double { … }
let sorted = names.sorted { $a, $b in a < b }
Structures, classes, enums and access control
struct Point {
var x: Double
var y: Double
mutating func translate(dx: Double, dy: Double) {
x += dx; y += dy
}
}
class Vehicle {
var currentSpeed = 0.0
func description() -> String { "speed \(currentSpeed)" }
}
enum Barcode {
case upc(Int, Int, Int, Int)
case qr(String)
}
open
> public
> package
(Swift 6) >
internal
> fileprivate
> private
Interfaces, default implementations and protocol-oriented design
protocol Drawable {
func draw(in ctx: CGContext)
}
extension Drawable {
func renderPNG() -> Data { … }
}
Note: Protocol-oriented programming encourages composing behaviour via protocols + extensions rather than deep class hierarchies.
Type-safe abstraction without sacrifice
struct Stack<Element> {
private var items: [Element] = []
mutating func push(_ item: Element) { items.append(item) }
mutating func pop() -> Element { items.removeLast() }
}
protocol Container {
associatedtype Item
var count: Int { get }
subscript(index: Int) -> Item { get }
}
extension Stack: Container { … }
Structured concurrency and data-race safety
func fetchImage() async throws -> UIImage {
let (url, _) = try await URLSession.shared.data(from: imageURL)
return UIImage(data: url)!
}
actor Counter {
private var value = 0
func increment() { value += 1 }
func read() -> Int { value }
}
Swift 6 tightens isolation rules and introduces compile-time data-race safety.
ARC, borrowing and copy-on-write
ARC inserts retain/release
calls at compile time for class
instances.
Structs and enums are value types using copy-on-write optimisation.
func process(buffer consume inout Data) { … }
Borrowing lets the compiler prove exclusivity, enabling aggressive optimisation and future stack allocation.
Meta-programming in modern Swift
@available
for version checks@discardableResult
to silence warnings@MainActor
for UI-bound APIs#stringify(x + y) // expands to "(x + y)"
#warning("Deprecated") // compiler diagnostic
@resultBuilder
struct HTMLBuilder { … }
func page(@HTMLBuilder body: () -> String) -> String { body() }
Xcode, swiftc and build flags
swiftc -warn-concurrency -strict-concurrency=complete main.swift -o app
Idiomatic patterns for production code
class
only when reference semantics are essential.internal
by default;
expose minimal public API.throw
/try
instead of optionals when failure is exceptional.switch
statements on enums
to profit from compiler checks.