Learn Swift – tutorial, cheatsheet & exercises
Swift is Apple's modern programming language for iOS, iPadOS, macOS, watchOS, tvOS, and visionOS. It is statically typed, compiles to fast machine code (LLVM), and puts great emphasis on safety: variables are always initialized, optionals make “no value” visible in the type system, arrays check their bounds, and memory is managed automatically via ARC, without a garbage collector.
The language is expressive and readable: type inference, closures, enums with values, protocols with default implementations, and a modern concurrency model (async/await, actors). With SwiftUI you describe user interfaces declaratively. Swift also runs on Linux, Windows, and on servers (Vapor), but it is above all the language of the Apple ecosystem.
Hello World in Swift
print("Hello World!")print outputs text: no main, no braces around the block, no semicolon. Run it with swift hallo.swift.
How to get started with Swift
The most convenient way to install Swift on a Mac is via Xcode (free in the Mac App Store). It includes the compiler, simulators for iPhone/iPad, interface tools, and playgrounds. If you only need the command-line tools: xcode-select --install. For Linux and Windows there are toolchains on swift.org.
swift --versionYou can run single files directly as a script or compile them to a binary:
# run as a script (interpreted via JIT)
swift hallo.swift
# compile and run
swiftc hallo.swift -o hallo
./halloPlaygrounds are great for learning and experimenting: in Xcode via *File > New > Playground* (or in the *Swift Playground* app on iPad and Mac). Every line shows its result immediately, without needing a whole project.
For larger programs and libraries you use the Swift Package Manager (SwiftPM). It is built into Swift and works without Xcode:
mkdir Hallo && cd Hallo
swift package init --type executable
swift run # build and run
swift test # run testsimport SwiftUI), on the other hand, exist only on Apple platforms.Swift learning path
Hello World, print & Strings
Your first Swift program, output, and string interpolation.
Variables, Constants & Types
let and var, type inference, and the most important basic types.
Conditions & Loops
if, switch with patterns, for-in, while, and ranges.
Functions & Argument Labels
Parameters, default values, return values, inout, and functions as values.
Arrays, Dictionaries & Sets
The three collection types, with value semantics and bounds checking.
Optionals
Swift's answer to null: a value is present or nil, enforced by the compiler.
Structs vs. Classes
Value types and reference types, methods, computed properties, and inheritance.
Enums with Associated Values
Enums that carry data: modeling states safely.
Protocols & Extensions
Describing behavior, default implementations, and extending existing types.
Closures & Higher-Order Functions
Anonymous functions, map/filter/reduce, and capturing values.
Error Handling
throws, try, do/catch, Result, and defer.
Swift Package Manager & Tests
Structuring projects, adding dependencies, and testing code.
ARC, weak & Retain Cycles
How Swift manages memory and how you avoid memory leaks.
Generics & Property Wrappers
Reusable code for many types, associated types, and property wrappers.
async/await, Tasks & Actors
The modern concurrency model: structured concurrency without callback chains.
SwiftUI: The Core Idea
Declarative interfaces: views as a function of state.
At a glance
More about Swift
Test & practice
Interactive quiz, timed exam with certificate, and exercises with an editor: