Preface
Since its surprise debut at WWDC in June 2014, Swift has rapidly redefined how we build software, combining safety, speed, and a clear, concise syntax to power everything from iOS, macOS, watchOS, and tvOS apps to server-side services on Linux and, since September 2020, native Windows development. Born from Chris Lattner's vision of a modern successor to Objective-C, Swift's modern features, such as optionals, generics, and closures, combined with its powerful type inference system, have made code more expressive, reliable, and enjoyable to write. While Apple's decision to open-source the language in 2015 and stabilize its ABI in Swift 5 has created a vibrant community around swift.org. In this book, we'll explore some of Swift's most advanced features to help you take your development to the next level, writing cleaner, more powerful, and highly optimized applications.
Who this book is for
This book is written for developers with an understanding of the Swift programming language who are looking to elevate their skills by exploring advanced topics and techniques. The examples provided are compatible with application development across all Apple platforms, including macOS, iOS, iPadOS, visionOS, and watchOS, as well as Linux and Windows development, unless otherwise specified.
What this book covers
Chapter 1, The Evolution of Swift, highlights the evolution of the Swift language, showing how its history shaped the features and design choices that developers use today. It also introduces swift.org, a key resource providing documentation, collaboration opportunities, and support for the Swift community.
Chapter 2, Closures and Result Builders, introduces closures, which are self-contained blocks of code that can capture and retain references to variables from their surrounding context. Additionally, we will look at result builders, a feature that allows developers to create custom Domain-Specific Languages (DSLs) used for defining complex data structures.
Chapter 3, Protocols and Protocol Extensions, explores protocols, highlighting how they can be used as full-fledged types that enable polymorphism through the unified interface they provide. It also introduces protocol extensions, which help reduce code duplication by offering default implementations for methods and properties. Additionally, the chapter covers the use of Any
to represent an instance of any type, and existential any, which allows storing any value that conforms to a specific protocol.
Chapter 4, Generics, explores generic types, which serve as the foundation for many types in the Swift standard library. Real-world examples demonstrate how to create and use generics effectively. Additionally, the chapter covers topics such as generic subscripts and associated types, illustrating how generics can enhance the flexibility of our code.
Chapter 5, Value and Reference Types, explains the differences between value types and reference types in Swift. This chapter includes practical examples of structures and classes, covering features such as copy-on-write for optimizing performance with large value types and recursive data types for reference types. Additionally, we will look at dynamic dispatch, which, while introducing some performance overhead, enables the flexibility needed for class hierarchies.
Chapter 6, Enumerations, looks at Swift enumerations and what makes them more powerful than enumerations in other languages. We will also look at how to use raw values and how pattern matching can simplify our code. Additionally, we will see how associated values allow Swift enumerations to represent complex data structures.
Chapter 7, Reflection, looks at what reflection is and how the Mirror API in Swift enables it. We will demonstrate how to use the Mirror API to inspect structures and classes, revealing details such as property names, values, and type information.
Chapter 8, Error Handling and Availability, looks at Swift's error handling mechanisms and how errors are defined using types conforming to the Error protocol. We will demonstrate how to throw errors in functions using the throws
keyword and handle them with do-catch
blocks. Additionally, we will discuss Swift's availability
and unavailability
attributes for conditional code execution based on platform versions.
Chapter 9, Regular Expressions, explores the power of regular expressions for pattern matching and text manipulation, from simple searches to complex data extraction. The chapter shows how Swift supports regular expressions with regular expression literals and the Regex
type. Additionally, Regex Builder
is introduced, which offers a clearer, more intuitive way to define patterns, and we will look into advanced features such as transforming and capturing matches using references.
Chapter 10, Custom Subscripting, explores how incorporating subscripts into custom types can improve readability and usability. We will examine how to create multi-parameter subscripts and discuss how to use subscripts appropriately, consistent with the Swift language itself.
Chapter 11, Property Observers and Wrappers, explores how property observers and wrappers automatically respond to changes in property values, which improves the responsiveness of our applications. We will examine how property observers trigger actions while property wrappers abstract property management into reuseable types.
Chapter 12, Dynamic Member Lookup and Key Paths, explores how dynamic member lookups allow properties to be resolved at runtime. Additionally, we will examine how key paths offer a type-safe way to access and manipulate properties using the \.
syntax.
Chapter 13, Grand Central Dispatch, explores the difference between concurrency and parallelism. We will see how Grand Central Dispatch uses both serial and concurrent queues to handle task execution. Additionally, we will highlight how it has tools for managing UI updates and scheduling tasks effectively.
Chapter 14, Structured Concurrency, explores how async and await offer intuitive ways to handle asynchronous tasks, allowing functions to pause and resume without blocking the main thread. We will examine how tasks and task groups further control and coordinate asynchronous operations. Additionally, we will look at actors and the role they play in managing state safety.
Chapter 15, Memory Management, examines how Swift uses Automatic Reference Counting (ARC) to manage memory for reference types such as classes. We will discuss how to use weak and unowned references to avoid strong reference cycles, which can hinder ARC from deallocating reference types, causing memory leaks.
Chapter 16, Advanced and Custom Operators, explores advanced bitwise operators, such as the AND, OR, XOR, and NOT operators, for manipulating variable bits and the right and left shift operators for shifting bits. We will also look at how to add operator methods to our custom types.
Chapter 17, Access Controls, looks at how access controls are used to ensure code security by restricting access to specific parts of the code base. We will examine the five access levels that Swift provides and how to apply them effectively. Additionally, we will see how applying the principle of least privilege will ensure that only the necessary parts of our code are accessible.
Chapter 18, Swift Testing, looks at Swift Testing, which is a powerful new testing framework introduced in Swift 6. We will explore the key building blocks, such as the @Test
attribute and the #expect
and #require
macros, as well as the use of traits for adding metadata.
Chapter 19, Object-Oriented Programming with Swift, looks at object-oriented programming and how its design principles can be effectively applied with Swift to create structured and reusable code.
Chapter 20, Protocol-Oriented Programming with Swift, explores the core principles of protocol-oriented programming with Swift and how it differs from traditional object-oriented programming. Additionally, we will examine how Swift's standard library is built with a protocol-oriented approach.
Chapter 21, Functional Programming with Swift, looks at Swift's support for functional programming concepts such as immutability, pure functions, and first-class functions. We will also explore advanced techniques such as function composition, currying, and recursion to manage complex operations effectively.
To get the most out of this book
To get the most out of this book, you will need a basic understanding of the Swift language and a basic understanding of modern development techniques. All code examples have been tested using Xcode 26 on a Mac; however, all examples should also work using Swift on Linux or Windows.
Download the example code files
The code bundle for the book is hosted on GitHub at...