Intro
Swift is Apples language for iOS, macOS, watchOS, tvOS, and visionOS. Modern syntax, strong type system, and great tooling.
Swift — what it is
EXAMPLE
// ===== The values =====
// - Safe by default: optionals model null; no NPE
// - Strong, expressive type system (generics, protocols, associated types)
// - Excellent SwiftUI for declarative UIs
// - Cross-platform on Linux + Windows (servers + scripts via Swift on Server)
// ===== Hello, world =====
print("hello, world")
// Run: swift hello.swift
// ===== A tiny SwiftUI app =====
import SwiftUI
struct ContentView: View {
@State private var count = 0
var body: some View {
VStack(spacing: 16) {
Text("Clicks: \(count)")
.font(.title)
Button("+1") { count += 1 }
.buttonStyle(.borderedProminent)
}
.padding()
}
}
@main
struct DemoApp: App {
var body: some Scene {
WindowGroup { ContentView() }
}
}
// ===== Modern features =====
// async/await:
func fetch(_ url: URL) async throws -> Data {
let (data, _) = try await URLSession.shared.data(from: url)
return data
}
// Result + throws:
enum AppError: Error { case notFound }
func find(_ id: Int) throws -> String {
guard id == 1 else { throw AppError.notFound }
return "Alex"
}
// Structs + value semantics:
struct Point { var x: Double; var y: Double }
var p = Point(x: 1, y: 2)
var q = p // copy, not reference
q.x = 99
// p.x is still 1
// ===== When Swift wins =====
// - All Apple platform apps
// - Performance-sensitive apps where you want value types
// - Server-side Swift (Vapor, Hummingbird) is real and good
// - When you want a modern language and Apple SDK access
// ===== When Swift hurts =====
// - Cross-platform (Apple SDKs only on Apple platforms)
// - Smaller library ecosystem outside Apple stack
// - Compile times on big codebases (improving but real)
// ===== Patterns to internalise =====
// - Optional types are first-class; use guard let early
// - Value types (struct) by default; class only when identity / shared mutation needed
// - Protocols + extensions over deep class hierarchies
// - SwiftUI for new UIs; UIKit for legacy + niche needs
// ===== Pitfalls =====
// - Force-unwrap (!) everywhere -> reintroduces crashes
// - Capturing self strongly in closures -> retain cycles ([weak self])
// - Massive view controllers (UIKit antipattern)
// - Mixing Combine + async/await without a clear strategy
Why it matters
Swift is one of the most pleasant modern languages and the only sensible choice for Apple platform apps. SwiftUI + async/await + structs covers most app code; protocols + generics give you reach for libraries. The trade-off is the ecosystem boundary; inside Apples walls, Swift is hard to beat.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
// Swift: Apple's language for iOS, macOS, watchOS, tvOS, server. // Static types, ARC, value semantics, modern concurrency.Try it Yourself »
Discussion
Loading…