Introduction
Who Is This Book For?
Themes
Terminology
Swift Style Guide
Revision History
Built-In Collections
Arrays
Arrays and Mutability
Array Indexing
Transforming Arrays
Array Slices
Dictionaries
Mutating Dictionaries
Some Useful Dictionary Methods
Hashable Requirement
Sets
Set Algebra
Index Sets and Character Sets
Using Sets Inside Closures
Ranges
Countable Ranges
Partial Ranges
Range Expressions
Recap
Optionals
Sentinel Values
Replacing Sentinel Values with Enums
A Tour of Optional Techniques
if let
while let
Doubly Nested Optionals
if var and while var
Scoping of Unwrapped Optionals
Optional Chaining
The nil-Coalescing Operator
Using Optionals with String Interpolation
Optional map
Optional flatMap
Filtering Out nils with compactMap
Equating Optionals
Comparing Optionals
When to Force-Unwrap
Improving Force-Unwrap Error Messages
Asserting in Debug Builds
Implicitly Unwrapped Optionals
Implicit Optional Behavior
Recap
Functions
Overview
Flexibility through Functions
Functions as Data
Functions as Delegates
Delegates, Cocoa Style
Delegates That Work with Structs
Functions Instead of Delegates
inout Parameters and Mutating Methods
Nested Functions and inout
When & Doesn't Mean inout
Properties
Change Observers
Lazy Stored Properties
Subscripts
Custom Subscripts
Advanced Subscripts
Key Paths
Key Paths Can Be Modeled with Functions
Writable Key Paths
The Key Path Hierarchy
Key Paths Compared to Objective-C
Future Directions
Autoclosures
The @escaping Annotation
withoutActuallyEscaping
Recap
Structs and Classes
Value Types and Reference Types
Mutation
Mutating Methods
inout Parameters
Lifecycle
Reference Cycles
Closures and Reference Cycles
Choosing between Unowned and Weak References
Deciding between Structs and Classes
Classes with Value Semantics
Structs with Reference Semantics
Copy-On-Write Optimization
Copy-On-Write Tradeoffs
Implementing Copy-On-Write
Recap
Enums
Overview
Enums Are Value Types
Sum Types and Product Types
Pattern Matching
Pattern Matching in Other Contexts
Designing with Enums
Switch Exhaustively
Make Illegal States Impossible
Use Enums for Modeling State
Choosing between Enums and Structs
Parallels between Enums and Protocols
Use Enums to Model Recursive Data Structures
Raw Values
The RawRepresentable Protocol
Manual RawRepresentable Conformance
RawRepresentable for Structs and Classes
Internal Representation of Raw Values
Enumerating Enum Cases
Manual CaseIterable Conformance
Frozen and Non-Frozen Enums
Tips and Tricks
Recap
Strings
Unicode
Grapheme Clusters and Canonical Equivalence
Combining Marks
Emoji
Strings and Collections
Bidirectional, Not Random Access
Range-Replaceable, Not Mutable
String Indices
Substrings
StringProtocol
Code Unit Views
Index Sharing
Strings and Foundation
Other String-Related Foundation APIs
Ranges of Characters
CharacterSet
Unicode Properties
Internal Structure of String and Character
String Literals
String Interpolation
Custom String Descriptions
Text Output Streams
Recap
Generics
Generic Types
Extending Generic Types
Generics vs. Any
Designing with Generics
Recap
Protocols
Protocol Witnesses
Conditional Conformance
Protocol Inheritance
Designing with Protocols
Protocol Extensions
Customizing Protocol Extensions
Protocol Composition
Protocol Inheritance
Protocols with Associated Types
Conditional Conformance with Associated Types
Existentials
Existentials and Associated Types
Type Erasers
Retroactive Conformance
Recap
Collection Protocols
Sequences
Iterators
Conforming to Sequence
Iterators and Value Semantics
Function-Based Iterators and Sequences
Single-Pass Sequences
The Relationship between Sequences and Iterators
Conforming List to Sequence
Collections
A Custom Collection
Array Literals
Associated Types
Indices
Index Invalidation
Advancing Indices
Custom Collection Indices
Subsequences
Slices
Subsequences Share Indices with the Base Collection
Specialized Collections
BidirectionalCollection
RandomAccessCollection
MutableCollection
RangeReplaceableCollection
Lazy Sequences
Lazy Processing of Collections
Recap
Error Handling
Error Categories
The Result Type
Throwing and Catching
Typed and Untyped Errors
Non-Ignorable Errors
Error Conversions
Converting between throws and Optionals
Converting between throws and Result
Chaining Errors
Chaining throws
Chaining Result
Errors in Asynchronous Code
Cleaning Up Using defer
Rethrowing
Bridging Errors to Objective-C
Recap
Encoding and Decoding
A Minimal Example
Automatic Conformance
Encoding
Decoding
The Encoding Process
Containers
How a Value Encodes Itself
The Synthesized Code
Coding Keys
The encode(to:) Method
The init(from:) Initializer
Manual Conformance
Custom Coding Keys
Custom encode(to:) and init(from:) Implementations
Common Coding Tasks
Making Types You Don't Own Codable
Making Classes Codable
Making Enums Codable
Decoding Polymorphic Collections
Recap
Interoperability
Wrapping a C Library
Package Manager Setup
Wrapping the CommonMark Library
A Safer Interface
An Overview of Low-Level Types
Pointers
Closures as C Callbacks
Making It Generic
Recap
Final Words