Version First Edition
Platform iOS 14
Language Swift 5.4
Editor Xcode 12.5

Master the Swift language with the Expert Swift book!

Swift is a rich language with a plethora of features to offer. Reading the official documentation or entry-level books is important, but it’s not enough to grasp the true power of the language.

Expert Swift is here to help, by showing you how to harness the full power of Swift. You’ll learn about advanced usages of protocols, generics, functional reactive programming, API design and more.

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

i

iii

iv

v

Section I: Core Concepts

The first section of this book covers the basic building blocks of the Swift language: The type system (enums, structs and classes), Protocols and Generics. We’ll start with a brief refresher of each topic and then jump right into the behind-the-scenes implementations.

The content of this section will expose the inner workings of the type system, as well as get you intimately familiar with protocols and generics.

Welcome to Expert Swift. In this chapter, you’ll learn about some of the motivations behind creating the Swift language, take a short but deep dive into the Swift toolchain flow and look at Swift Intermediate Language, SIL. You’ll develop a simple language feature, ifelse, to explore some of the facilities Swift offers for creating powerful, expressive abstractions.

1

Types are essential to building Swift programs. The Swift compiler type checks your code to verify correctness, ensure safety and enable greater optimization. You’ll gain experience about the different nominal types and mutation with several small examples. You’ll also implement mutable value semantics for a QuadTree type using copy-on-write dynamic storage.

2

Protocols

Free

In this chapter you'll go through a brief refresher on the basics of protocols as well as some of their more rarely used features. You’ll also learn about common patterns that use protocols as well as some useful gotchas and edge cases to keep in mind.

3

Generics

Free

In this chapter you’ll get intimately familiar with generics by continuing to work on the networking library you started in the previous chapter. You’ll learn how to write generic functions, classes and structs, how to use protocols with associated types, what type erasure is and how to put all that together to make a coherent API.

4

Section II: Standard Library

This sections covers the base layer of writing Swift programs: Numerics, Ranges, Strings, Sequences, Collections, Codable and the less obvious, but very important topic - Unsafe.

As you’d expect from an advanced book, we don’t only explain these topics, but also investigate how they’re built, how they’re represented, and how to use them effectively.

Swift is a platform-agnostic, general-purpose programming language that supports various numeric types with differing space, range, accuracy and performance characteristics. Building two apps (BitViewer and Mandlebrot), you’ll see how Swift simplifies programming with protocols and generics. You’ll also look at range types and how operators and generics once again come to the rescue in implementing these language features.

5

Sequence, Collection and related protocols form the backbone of the standard library for types like Array, Dictionary and Set. You’ll see how these protocols allow you to write generic algorithms that operate across families of collections. The standard library offers many ways to quickly build custom sequences and collections. You’ll use these to build several examples including a custom mutable collection to implement Conway’s Game of Life. You’ll also create a chunking algorithm that can be used with any collection type.

6

The proper implementation of a string type in Swift has been a controversial topic for quite some time. The design is a delicate balance between Unicode correctness, encoding agnosticism, ease-of-use and high-performance. Almost every major release of Swift has refined the String type to the awesome design we have today. You’ll learn how you can most effectively use strings, what they really are, how they work and how they’re represented.

7

When developing your app, you’ll often deal with a myriad of data models and various external pieces of data that you’ll want to represent as data models in your app. In this chapter, you’ll quickly browse through the basics of Codable, and then focus on the advanced materials down the dark corners of codable types.

8

Swift is a memory-safe and type-safe language. In some cases, you might need your code to be extremely optimized, in which case the tiny overhead added by the safety checks from Swift might be too expensive. You might be dealing with a huge stream of real-time data, manipulating large files or other large operations that deal with large data. In this chapter you'll learn how to use unsafe Swift to directly access memory through a variety of pointer types and how to interact with the memory system directly.

9

Section III: Techniques

The final section of this book covers advanced techniques to super-charge your Swift powers, and use all of what Swift has to offer.

We’ll cover topics like Higher order functions, Functional reactive programming, Objective-C interoperability, using Instrumentation, and API design.

higher-order functions can simplify your code significantly by making it more readable, a lot shorter and easier to reuse. You'll learn what are higher order functions, what is currying and examine examples of how they're used in the standard library.

10

In this chapter you'll learn the most important and refined concepts of functional reactive programming and how you can apply these concepts to your apps.

11

Like it or not, Objective-C is still a heavily used language in legacy codebases and apps that have been in production for many years. In your own apps, you’ll often have a sizable Objective-C codebase that just doesn’t feel at home inside your Swift code or want to use some of your shiny new Swift code in your Objective-C code. In this chapter, you'll learn how to create a wholesome experience for consumers of both the Objective-C and Swift portions of your codebase in a way that feels as if it were designed for either.

12

Being a great iOS software engineer isn’t only about being a grandmaster of the Swift language. It’s also about knowing which tools the platform puts at your disposal, how to use them to sharpen your skills and how to identify areas of improvement in your code. In this chapter you'll learn about advanced features of the Instruments app, and how to use it to improve your code.

13

Explore a few topics to enhance your skillset and intuition for designing great APIs. Topics like Documentation, Encapsulation, versioning, and several powerful language features.

14

Meet the team

Contributors

Who is this book for

This book is for intermediate Swift developers who already know the basics of Swift and are looking to deepen their knowledge and understanding of the language.

Concepts covered in this book

  • Protocols & Generics
  • Numerics & Ranges
  • Sequences & Collections
  • Unsafe
  • Functional Reactive Programming
  • Objective-C Interoperability
  • Library & API Design

Version history

First Edition · iOS 14, Swift 5.4, Xcode 12.5

v1.0 · May 19 2021 · Marin Bencevic, Ray Fix, Shai Mishali & Ehab Yosry Amer
Pro

Expert Swift

By Marin Bencevic, Ray Fix, Shai Mishali and Ehab Yosry Amer

Take your Swift skills to the next level with the Expert Swift book.

Read for Free with an Ultimate Pro Subscription* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.
See buying options
Hide buying options

All videos. All books.
One low price.

A raywenderlich.com subscription is the best way to learn and master mobile development — plans start at just $19.99/month!

Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.