Version Sixth Edition
Platform iOS 14
Language Swift 5.3
Editor Xcode 12

This book is for complete beginners to Swift and coding. No prior programming experience necessary!

This is a book for complete beginners to Apple’s modern programming language — Swift.

All the code in the book works inside of Xcode’s easy-to-use playgrounds. That means you can focus on core Swift language concepts, such as classes, protocols, and generics, instead of getting bogged down in the details of building apps.

This is a companion book to the UIKit Apprentice; the UIKit Apprentice focuses on building apps, while Swift Apprentice focuses on the Swift language itself.

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

Section I: Swift Basics

The chapters in this section will introduce you to the very basics of programming in Swift. From the fundamentals of how computers work up to language structures, you’ll cover enough of the language to be able to work with data and organize your code’s behavior.

The section begins with some groundwork to get you started. Once you have the basic data types in your head, it’ll be time to do things with that data, and finally, you’ll learn about an essential data type, optionals, that let you express potentially missing data.

These fundamentals will get you Swiftly on your way, and before you know it, you’ll be ready for the more advanced topics that follow. Let’s get started!

This is it, your whirlwind introduction to the world of programming! You’ll begin with an overview of computers and programming, and then say hello to Swift playgrounds, which are where you’ll spend your coding time for the rest of this book. You’ll learn some basics such as code comments, arithmetic operations, constants and variables. These are some of the fundamental building blocks of any language, and Swift is no different.

1

You’ll learn about handling different types, including strings that allow you to represent text. You’ll learn about converting between types, and you’ll also get an introduction to type inference, which makes your life as a programmer a lot simpler. You’ll learn about tuple types which allow you to group values of any type together.

2

You’ll learn how to make decisions and repeat tasks in your programs using syntax to control the flow. You’ll also learn about Booleans, which represent true and false values, and how you can use these to compare data.

3

Continuing the theme of code not running in a straight line, you’ll learn about another loop known as the `for` loop. You’ll also learn about switch statements that are particularly powerful in Swift.

4

Functions

Free

Functions are the basic building blocks you use to structure your code in Swift. You’ll learn how to define functions to group your code into reusable units.

5

Optionals

Free

This chapter covers optionals, a special type in Swift that represents either a real value or the absence of a value. By the end of this chapter, you’ll know why you need optionals and how to use them safely.

6

Section II: Collection Types

So far, you’ve mostly seen data in the form of single elements. Although tuples can have multiple pieces of data, you have to specify the size upfront; a tuple with three strings is a completely different type from a tuple with two strings, and converting between them isn’t trivial. In this section, you’ll learn about collection types in Swift. Collections are flexible “containers” that let you store any number of values together.

There are several collection types in Swift, but three important ones are arrays, dictionaries and sets. You’ll learn to apply custom operations and loop over collection types. Finally, you’ll revisit strings, which are collections of characters.

All the collection types share similar interfaces but have very different use cases. As you read through these chapters, keep the differences in mind, and you’ll begin to develop a feel for which type you should use when.

Arrays are the most common collection type you’ll run into in Swift. Arrays are typed, as are regular variables and constants, and store multiple values like a single list.

7

Swift has an object you can use to break up code into reusable chunks: a closure. These are particularly important when dealing with collections.

8

Finally, you will revisit strings, which are bi-directional collections of Unicode characters.

9

Section III: Building Your Own Types

You can create your own type by combining variables and functions into a new type definition. When you create a new type, you give it a name; thus, these custom types are known as named types. Structures are a powerful tool for modeling real-world concepts. You can encapsulate related concepts, properties and methods into a single, cohesive model.

Swift includes four kinds of named types: structures, classes, enumerations and protocols. You’ll learn here how other named types use the concepts of methods and properties, how they differ, and where you want to use each.

You’ll also learn about generics, which are types and methods that take as input other types instead of just methods, as well as custom types to build larger and complex things!

This chapter introduces the first named type--structures. Structures are types that can store named properties and define actions and behaviors.

10

In this chapter, you’ll learn about stored and computed properties, along with some tricks, such as how to monitor changes in a property’s value and delay initialization of a stored property.

11

Methods are merely functions that reside in a structure. You’ll take a closer look at how methods and initializers help you build more complex structures.

12

Structures introduced you to your first named type, which lets you define your own named types. In this chapter, you’ll get acquainted with classes, which are much like structures.

13

This chapter introduces the finer points of classes and helps you understand how you can create, use and manage complex classes.

14

In this chapter you'll learn about enumerations, a type that groups related values. You’ll also finally discover what an optional is under the hood.

15

Protocols are a type that can bridge common behaviors between structs, classes, and enums by defining an interface or template for an actual concrete type.

16

In this chapter, you’ll learn what generics are, learn how to write your own generic code, and loop back and look at the generic types in Swift - dictionaries, arrays, and optionals - from this new perspective.

17

Section IV: Advanced Topics

You’ve made it to the final section of this book! In this section, you’ll delve into some essential but more advanced topics to round out your Swift apprenticeship.

After reading this section, you’ll be on your way to being a knowledgable Swift developer by digging into topics even those most advanced programmers use.

You’ll learn to hide complexity in your apps and organize your code through tools like access control and testing. You’ll also work on pattern matching, error handling, and memory management, as well as encoding and decoding types.

And level up how you approach your code by customizing operators and subscripts, as well as leveraging protocol-oriented programming while using constraints to make generic code more useful.

Swift gives you powerful tools for hiding complexity and organizing your code into easier to digest units. This chapter details how to do that.

18

You’ll learn how you can define your own operators and subscripts to make your types feel even more like built-in language constructs.

19

With pattern matching, you can accomplish more — with less typing. You’ll master all of its many forms in this chapter.

20

In the real world, you can’t avoid some errors. Gracefully handling errors is what sets apart mediocre code from great code.

21

You will learn about the type serialization system introduced in Swift 4 with particular emphasis on the JSON format.

22

This chapter digs into the details of Swift memory management examining the relationship between objects. It shows you how to avoid common leaks.

23

Value semantics have a clear advantage over reference semantics in terms of the local reasoning but can lead to inefficiency for large objects. This chapter shows you how to get the best of both worlds.

24

From the standard library to user authored generics, Swift is a protocol-based language. In this chapter, you’ll see how to get all of the benefits associated with object-oriented programming while being able to avoid most of the difficulties.

25

Learn how to use constraints to make generic code more useful and how to hide implementation details with opaque return types.

26

Meet the team

Contributors

Who is this book for

This is a book for complete beginners to Apple’s modern programming language — Swift.

Concepts covered in this book

All the code in the book works inside of Xcode’s easy-to-use playgrounds. That means you can focus on core Swift language concepts, such as classes, protocols, and generics, instead of getting bogged down in the details of building apps.

This is a companion book to the UIKit Apprentice; the UIKit Apprentice focuses on building apps, while Swift Apprentice focuses on the Swift language itself.

Version history

Sixth Edition · iOS 14, Swift 5.3, Xcode 12

v6.0 · Oct 1 2020 · Cosmin Pupăză, Matt Galloway, Ben Morrow, Alexis Gallagher, Ehab Yosry Amer & Eli Ganim

Fifth Edition · iOS 13, Swift 5.1, Xcode 11

v5.0 · Oct 1 2019 · Ehab Yosry Amer, Cosmin Pupăză, Eli Ganim, Matt Galloway, Alexis Gallagher & Ben Morrow

Swift Apprentice

By Cosmin Pupăză, Matt Galloway, Ben Morrow, Alexis Gallagher, Ehab Yosry Amer and Eli Ganim

Beginning programming with Swift! This book takes you from beginner to advanced in Swift: Apple’s modern programming language for iOS.

Read for Free with an Ultimate Beginner Subscription* * Includes this and all other Beginner 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.