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.
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.
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.
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 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.
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.
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.
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.
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.
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.
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.
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.