Learn Swift

Safe, fast, modern language for Apple platforms, servers and beyond

28 lessons · 6 hours · 4 levels · covers Swift 6.2

Swift is Apple's open-source, compiled language with value types, optionals, protocols and generics, and a concurrency model that checks data races at compile time. It is the main language for iOS, macOS, watchOS and visionOS apps and is also used on Linux servers. Learn it to build native Apple apps with SwiftUI and to write fast, safe code that the compiler helps you get right.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics10 lessons · 2 hours in total
2. PracticalUse it productively at work, with the usual idioms18 lessons · 3 hours in total
3. AdvancedThe less common features, performance and trade-offs24 lessons · 5 hours in total
4. ExpertInternals and the edge cases experts know28 lessons · 6 hours in total

The Swift tree

SwiftConstants, variables and typesConstants, variablesand typesStrings and interpolationStrings andinterpolationif, switch and loopsif, switch and loopsOptionalsOptionalsArrays, dictionaries and setsArrays, dictionariesand setsFunctions and argument labelsFunctions andargument labelsClosuresClosuresStructures and value semanticsStructures and valuesemanticsClasses and reference semanticsClasses andreference semanticsEnumerations and associated valuesEnumerations andassociated valuesComputed properties and observersComputed propertiesand observersProtocolsProtocolsExtensionsExtensionsErrors: throw, try and catchErrors: throw, try andcatchGenerics and constraintsGenerics andconstraintsCodable and JSONCodable and JSONasync/await and Taskasync/await andTaskPackages, access control and testsPackages, accesscontrol and tests
Essentials and Practical shown (18 of 28 lessons); the full list is below. Click a lesson to read it.

All 28 lessons, in order

Getting started

  1. Constants, variables and types · Essentials · 10 min

    Declare values with let and var, rely on type inference, and use Swift's basic types.

  2. Strings and interpolation · Essentials · 8 min

    Build strings with interpolation and multi-line literals, and understand that Swift strings are Unicode-correct.

  3. if, switch and loops · Essentials · 10 min

    Branch with if and switch, loop with for-in, while and ranges, and exit early with guard.

  4. Optionals · Essentials · 12 min

    Represent missing values with Optional, and unwrap them safely with if let, guard let, ?? and optional chaining.

  5. Arrays, dictionaries and sets · Essentials · 12 min

    Store values in Array, Dictionary and Set, and transform them with map, filter and reduce.

Functions and closures

  1. Functions and argument labels · Essentials · 10 min

    Define functions with argument labels, default values, variadic and inout parameters, and multiple returns via tuples.

  2. Closures · Essentials · 12 min

    Write closure expressions, use trailing-closure and shorthand syntax, and understand capture and @escaping.

Structs, classes and enums

  1. Structures and value semantics · Essentials · 12 min

    Model data with structs: stored properties, memberwise initializers, methods and mutating.

  2. Classes and reference semantics · Essentials · 12 min

    Use classes for shared, identity-bearing objects: initializers, inheritance, overrides and the === operator.

  3. Enumerations and associated values · Essentials · 12 min

    Define enums with raw values, associated values and methods, and match them exhaustively with switch.

  4. Computed properties and observers · Practical · 10 min

    Use computed properties, lazy properties, property observers and access control on setters.

Protocols and generics

  1. Protocols · Practical · 14 min

    Define protocols with property and method requirements, conform types to them, and add default implementations.

  2. Extensions · Practical · 8 min

    Add methods, computed properties, initializers and protocol conformances to existing types with extensions.

  3. Generics and constraints · Practical · 12 min

    Write generic functions and types, constrain type parameters with protocols and where clauses, and use associated types.

  4. some vs any: opaque and boxed types · Advanced · 12 min

    Choose between opaque types (some P) and existential types (any P) and understand their cost and limits.

  5. Property wrappers and result builders · Advanced · 14 min

    Write property wrappers to reuse property logic, and understand result builders behind SwiftUI's view syntax.

Errors, Codable and packages

  1. Errors: throw, try and catch · Practical · 12 min

    Throw and handle errors with do/catch, try?, try!, typed throws and the Result type.

  2. Codable and JSON · Practical · 12 min

    Encode and decode JSON with Codable, JSONEncoder and JSONDecoder, CodingKeys and decoding strategies.

  3. Packages, access control and tests · Practical · 14 min

    Structure code into modules with Swift Package Manager, control visibility with access levels, and test with Swift Testing.

Concurrency

  1. async/await and Task · Practical · 14 min

    Call and write async functions, start work from synchronous code with Task, and combine async with throws.

  2. async let, task groups and cancellation · Advanced · 14 min

    Run child tasks in parallel with async let and task groups, and make long work respond to cancellation.

  3. Actors and the main actor · Advanced · 14 min

    Protect mutable state with actors, run UI code on @MainActor, and understand actor reentrancy.

  4. Sendable and Swift 6 data-race safety · Advanced · 15 min

    Understand Sendable, how the Swift 6 language mode rejects data races at compile time, and how to fix common errors.

  5. Isolation: regions, sending, nonisolated · Expert · 18 min

    Reason about isolation inheritance, region-based isolation, the sending keyword and how async functions choose where to run.

Memory and internals

  1. ARC, weak and unowned · Advanced · 12 min

    See how automatic reference counting frees class instances, and break retain cycles with weak and unowned references.

  2. Copy-on-write and value-type performance · Expert · 16 min

    Implement copy-on-write for your own value types and avoid accidental copies, retain/release traffic and existential overhead.

  3. Noncopyable types and ownership · Expert · 16 min

    Use ~Copyable types to model unique resources, and control ownership with borrowing, consuming and consume.

  4. Macros · Expert · 16 min

    Use freestanding and attached macros, understand how they expand at compile time, and know what writing one involves.