Advanced Swift

A deep dive into Swift’s features, from low-level programming to high-level abstractions.

by Chris Eidhof, Ole Begemann, Florian Kugler, and Ben Cohen

See Buying Options

The videos accompanying Advanced Swift build on top of the material explained in the book. Through live coding, we show how to apply the different topics in real code. Here is the full table of contents for the videos:

C Interoperability   1h12min

In this video we create a wrapper around Cairo, a C library for drawing with vector graphics.

  • Introduction   0:00

    We create a new Swift Package and import the Cairo C library.

  • Wrapping Cairo Types in Classes   13:17

    We create Swift classes to help with type-safety and memory management.

  • Protocols vs. Subclassing   31:52

    We explore the trade-offs between modeling Cairo's type hierarchy using protocols vs. subclassing.

  • Working with <code>Unmanaged</code>   48:04

    We wrap a streaming SVG API and perform manual memory management using Unmanaged.

  • Testing on Linux using Docker   1:02:37

    We show how to test the Swift Cairo wrapper on Linux by running it inside a container.

String Parsing   1h

We extend the CSV parsing example from the book to support more features and improve its performance.

  • Introduction   0:00

    We build the parser from the book and discuss its trade-offs.

  • Encoding Parsing State in Functions   18:13

    We show how modeling state using different functions makes the parser more maintainable.

  • Parsing Performance   39:52

    We measure and improve the performance of our parser by working on different string views.

Collection Protocols   58min

We show how to work with Swift's built-in Collection protocols by creating a custom SortedArray collection.

  • Introduction   0:00

    We create a naive implementation of a sorted array.

  • Improving SortedArray using Binary Search   07:03

    We implement binary search on SortedArray to make lookups and insertions much faster.

  • Conforming to Collection Protocols   17:37

    We conform to Collection, BidirectionalCollection and RandomAccessCollection, and discuss why we can't conform to MutableCollection and RangeReplaceableCollection.

  • Protocol Constraints and Conditional Conformance   40:57

    We work with protocol constraints to add conditional functionality, and show how to use conditional conformance.

  • Adding a SortedCollection Protocol   42:55

    We create a custom SortedCollection protocol on top of Collection and move most of the functionality from our SortedArray into the new protocol.

  • Conforming SortedSet to SortedCollection   50:07

    We create a new type, SortedSet, and conform it to our new protocol. With little effort we get a lot functionality.

Functions   36min

We build a validation library using higher-order functions.

  • Introduction   0:00

    We define our Validator type and show how to use it.

  • Generic Validation Helpers   7:34

    We implement helper methods to create basic validators for checking a single property.

  • Combining Validators   16:29

    We create a function to combine multiple validators into a single validator.

  • Lifting Validators   22:18

    We show how to lift validators from one type to a containing type by using key paths.

  • Transforming The Error Message   27:25

    We add a helper function that allows us to override the error message for a specific validator.

  • Throwing Validation Errors   31:39

    We show how to convert a validator into a function that throws whenever a validation error happens.

Encoding & Decoding Graphs   46min

We show different representations of graphs and how they work with Codable.

  • Introduction   0:00

    We build a class-based graph data structure and show why it can't be serialized using Codable.

  • Defining the Graph Struct   9:51

    We implement the graph as a struct without reference types.

  • Inserting into the Graph   13:15

    We show how to build graphs using the struct-based API.

  • Transforming Node to Graph   20:41

    We implement a function that automatically transforms a class-based graph into a struct-based graph.

  • Transforming Graph to Node   31:41

    We also implement the reverse transformation, from a struct-based graph back to a class-based graph.

  • Improving the Graph API Using an Opaque Type   42:59

    We hide implementation details by creating an opaque wrapper type for our object identifiers.