Lessons
Protocols and protocol extensions
Protocol definition, conformance, protocol extensions (default implementations), protocol composition (&), and Equatable/Hashable/Comparable.
What to look for:
- Protocol conformance is explicit: struct Dog: Animal — unlike Go's implicit satisfaction
- Protocol extension: add a default implementation in extension MyProtocol { ... }
- Protocol composition: func process(_ item: Drawable & Saveable) — requires both
- Equatable/Hashable auto-synthesised for structs/enums with all-equatable properties
Generics: type parameters and constraints
Generic functions (<T: Comparable>), generic types, where clauses, some (opaque) vs any (existential), and associated types.
Prerequisite: Lesson 1
What to look for:
- <T: Equatable> constrains T to types that support == comparison
- some View: opaque — compiler knows the concrete type; zero boxing cost (used in SwiftUI)
- any Drawable: existential — runtime type erasure; enables heterogeneous collections
- associated type in protocols: placeholder type resolved by conforming type
Error handling: throws, try, do-catch
Error protocol, throwing functions, try/try?/try!, do-catch, and Result<T, E> as alternative.
Prerequisite: Lesson 2
What to look for:
- throws: marks a function that can fail — callers must handle with try
- try? converts a thrown error to nil — use when you don't care about the specific error
- try! crashes on error — use only when failure is truly impossible
- do-catch must catch all thrown types — or add a catch { } catch-all at the end
Collections: Array, Dictionary, and Set
Array operations, Dictionary CRUD and merging, Set operations, map/filter/reduce/compactMap/flatMap, and lazy sequences.
Prerequisite: Lesson 3
What to look for:
- compactMap: like map but discards nil results — great for optional transformation
- Dictionary access returns Optional: dict["key"] returns String? — may be nil if missing
- Set: unordered, unique elements — O(1) contains, union, intersection, subtract
- lazy: .lazy.filter{...}.map{...} — doesn't compute until iterated; efficient for large sequences
Codable: JSON encoding and decoding
Codable = Encodable & Decodable, auto-synthesis, CodingKeys for custom JSON keys, nested types, and JSONDecoder configuration.
Prerequisite: Lessons 1–4
What to look for:
- struct User: Codable — synthesised automatically if all properties are Codable
- CodingKeys enum maps Swift property names to JSON keys (camelCase ↔ snake_case)
- JSONDecoder().keyDecodingStrategy = .convertFromSnakeCase — auto-maps without CodingKeys
- Nested Codable types work automatically — the compiler recurses through the structure
How to use this track: Each lesson links to the Swift reference pages. For protocols and generics, Apple's WWDC 2022 videos "Embrace Swift generics" and "Design protocol interfaces in Swift" are essential companion viewing.
Track quiz
4 questions to check your understanding.
How is Swift protocol conformance different from Go interface satisfaction?
What does try? do when applied to a throwing function?
What does compactMap do differently from map?
What does struct User: Codable do?