In 2010, Chris Lattner typed
mkdir shiny on his laptop — and that little passion project would ultimately grow into the Swift language as you know it today. Since then, Swift has captured the hearts of millions of developers, and proven itself as a mature, powerful, multi-platform language.
Now, there’s quite a difference between being able to execute
print("Hello, world!"), and being able to leverage the more esoteric features of the language with confidence!
Fortunately, Swift embraces the philosophy of progressive disclosure, which means you’re exposed to only the language complexity you need. You can learn about things like modules, access control, objects, static methods, protocols, generics and escaped characters as they become necessary to what you’re trying to do.
But — how do you get from “Hello, world!”… to using Swift like the experts do?
Beyond the Basics of Swift
As you advance in your career with Swift, you’ll quickly get to a point where you’ll need to know things about the finer details of the Swift standard library, the risks (and benefits) of
unsafe, as well as advanced language features such as higher-order functions, Obj-C interoperability, and even reactive and functional programming concepts — among other things.
Does that sound overwhelming? That’s okay – understanding the language at a deep level isn’t hard. You simply need a friendly guide to walk you through advanced Swift in action, using real-world examples in sample apps that you’d want to use yourself.
That guide is here now, in the form of our latest book: Expert Swift!
Level Up Your Swift Skills
Most of the books you know and love from raywenderlich.com are written in our “by Tutorials” style; however, this book is a little different. Since this is an advanced book, you won’t have a tutorial to accompany each chapter. Instead, you’ll go deep by covering low-level concepts as well as high-level abstractions, by using real-world sample projects to illustrate how the topics in each chapter would appear in real life.
This book is a curated collection of advanced topics that every intermediate to advanced developer can benefit from. Because most chapters in the book cover standalone concepts, Expert Swift is the ultimate reference book to deepen your understanding of specific Swift topics. Read a chapter or two, then take the skills and knowledge you’ve gained back into your existing app development — and reap the rewards.
Trying to explain advanced topics, like the ones this book covers, is not an easy task, so you should expect some longer chapters. Acquiring this advanced knowledge is worth it though, and as you read through the book, you’ll find yourself wanting to jump to your existing apps and implement the techniques you learn.
What’s Inside the Book?
Each chapter of this book presents some theory on the topic at hand, along with Swift code to demonstrate the practical applications of what you’re learning.
The chapters are independent, meaning you can read them in any order you’d like. The only exception to this are the Protocols (chapter 3) and Generics (chapter 4) chapters, which share one sample project.
Some chapters provide a starter project, while in some you’ll use playgrounds to write code from scratch.
This book is split into three main sections, totalling 14 chapters overall:
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. You’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:
Introduction: Learn the motivations behind creating the Swift language, take a short but deep dive into the Swift toolchain flow, Swift Intermediate Language (SIL), and develop a simple language feature,
ifelse, to explore some of the facilities Swift offers for creating powerful, expressive abstractions.
- Types & Mutation: 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.
- Protocols: Get 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.
- Generics: 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.
Section II: Standard Library
This section 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, you won’t just see these topics explained; you’ll see each advanced Swift concept in action as you investigate how they’re built, how they’re represented, and how to use them effectively:
- Numerics & Ranges: Through 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.
- Sequences, Collections and Algorithms: Sequence, Collection and related protocols form the backbone of the standard library for types like Array, Dictionary and Set. 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.
- Strings: 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. You’ll learn how you can most effectively use strings, what they really are, how they work and how they’re represented.
- Codable: 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.
- Unsafe: 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. 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.
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.
You’ll cover topics like higher order functions, functional reactive programming, Objective-C interoperability, using Instruments, and achieving API design:
- Higher-Order Functions: Higher-order functions can simplify your code significantly by making it more readable, a lot shorter and easier to reuse. You’ll learn what higher order functions are, what currying is, and examine examples of how they’re used in the standard library.
- Functional Reactive Programming: 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.
- Objective-C Interoperability: 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.
- Instrumentation: 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.
- API Design Tips & Tricks: Explore a few topics to enhance your skillset and intuition for designing great APIs. Topics like Documentation, Encapsulation, versioning, and several powerful language features.
What Else Do You Need?
To follow along with this book, you’ll need the following:
- A Mac running macOS Catalina 10.15 or later, with the latest point release and security patches installed. This is so you can install the latest version of the required development tool: Xcode.
- Xcode 12.5 or later. You can download the latest version of Xcode for free from the Mac App Store, here: https://apps.apple.com/app/xcode/id497799835
The code covered in this book depends on Swift 5.4 and Xcode 12.5 — you may get lost if you try to work with an older version or work outside the playground environment that this book assumes.
How to Get Your Own Copy
There are two ways you can get this book:
- Buy it individually: If you want to build up a custom library without a subscription, you can choose to buy the book individually. It’s available now for just $59.99 in our online store.
- Get it as part of our subscriptions: Enjoy access to our videos and mobile development books in our Ultimate Beginner and Ultimate Pro subscription, including Android Apprentice! Right now, you can get a full year’s subscription and save 20% off the cost of a regular monthly subscription. It’s simply the best investment for your mobile development career.
We hope you enjoy this book! Feel free to reach out to us if you have any questions.
Come meet the Swift dream team that created this book:
Ehab Amer [Author]: Ehab is a very enthusiastic Lead iOS developer with a very diverse experience from building games to enterprise applications and POCs with new technologies. In his spare time, TV shows take the majority of his time, followed by video games. When away from the screen, he goes with his wife and friends to explore the underwater world through diving.
Marin Bencevic [Author]: Marin is a computer vision researcher working on medical images. He is also an iOS developer who likes to work on cool iOS apps and games, nerd out about programming, learn new things and then blog about it. He also has a chubby cat.
Ray Fix [Author]: Ray works on next-generation microscopes made for iPad at Discover Echo Inc. in San Diego, California. Ray enjoys learning new things and is excited about math, data, visualization, machine learning and computer vision. Swift is his problem-solving language of choice and he has been using it and teaching others about it since its 2014 public release.
Shai Mishali [Author]: Shai is an experienced, award-winning iOS specialist; as well as an international speaker, and a highly active open-source contributor and maintainer on several high-profile projects – namely, the RxSwift Community and RxSwift projects, but also releases many open-source endeavors around Combine such as CombineCocoa, RxCombine and more. As an avid enthusiast of hackathons, Shai took 1st place at BattleHack Tel-Aviv 2014, BattleHack World Finals San Jose 2014, and Ford’s Developer Challenge Tel-Aviv 2015. You can find him on GitHub and Twitter as @freak4pc.
Morten Faarkrog [Tech Editor]: Morten is a Technical Director & Solution Architect at a consultancy in Denmark, where he works with architecting larger software solutions across a range of companies. Morten has a background as an iOS developer and still loves tinkering with the Swift language in his spare time.
John Hagemann [Editor]: John is a communications specialist and analyst who enjoys putting technical subject matter into plain talk. He lives in Washington state with his son.
Eli Ganim [Final Pass Editor]: Eli is an iOS engineer from Israel, who’s passionate about teaching, writing and sharing knowledge with others.
Start Reading for Free Today
Expert Swift is out today — and even better, you can start reading the entire first section of the book for free:
Like every one of our books, Expert Swift has been automatically added to the collections of all Ultimate Pro subscribers.
On behalf of the book team, we wish you much success as you leverage more of the elegance and power of Swift in your own projects!