Swift Apprentice Updated for Swift 4.2

Swift Apprentice has been completely updated for Swift 4.2 as part of our iOS 12 Launch Party! By Chris Belanger.

Save for later
Share

Today’s book release is the Swift Apprentice, Fourth Edition.

This will be a free update for existing Swift Apprentice digital edition customers. This is our way of thanking our past customers for their support!

Oh — don’t own the Swift Apprentice yet? Read on to see how you can get your own copy and take advantage of our iOS 12 Launch Party discount!

What is the Swift Apprentice?

If you’re a complete beginner to programming, this is the book for you! There are short exercises and challenges throughout the book to give you programming practice and test your knowledge along the way.

In a little over four years, Swift has gone from being a secret project at Apple, Inc. to a full-blown, open source community-driven language. It continues to refine its core goal of being a general purpose language that supports safety, speed and expressiveness.

Despite its advanced, industrial-strength nature, Swift is a great choice for the beginning programmer, since Xcode offers a sandbox-type environment where you can directly execute Swift statements to try out various components of the language — without having to create a whole app first.

Developers around the world use Swift to build thousands of amazing apps for the iPhone, iPad, Mac, Apple TV and the Apple Watch. That means what you learn in this book will be extremely useful as you expand your development skills and in your work as an app developer.

This book is divided into four sections:

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 all the way 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.

  • Chapter 1, Expressions, Variables & Constants: 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.
  • Chapter 2, Types & Operations: You’ll learn about handling different types, including strings which allow you to represent text. You’ll learn about converting between types and you’ll also be introduced to type inference which makes your life as a programmer a lot simpler. You’ll learn about tuples which allow you to make your own types made up of multiple values of any type.

Once you have the basic data types in your head, it’ll be time to do things with that data:

  • Chapter 3, Basic Control Flow: You’ll learn how to make decisions and repeat tasks in your programs by 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.
  • Chapter 4, Advanced Control Flow: 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 which are particularly powerful in Swift.
  • Chapter 5, Functions: 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.

The final chapter of the section loops a very important data type:

  • Chapter 6, Optionals: 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.

Section II: Collection Types

Stored data is a core component of any app, whether it’s a list of friends in your social networking app or a set of unlockable characters in your hit game. In this section, you’ll learn how to store collections of data in Swift.

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 up front; 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 about these here:

  • Chapter 7, Arrays, Dictionaries, and Sets: There are several collection types in Swift, but three important ones are arrays, dictionaries and sets. You’ll learn about these here.
  • Chapter 8, Collection Iterations With Closures: Next you’ll learn how to apply custom operations and loop over collection types.
  • Chapter 9, Strings: Finally, you will revisit strings, which are actually bi-directional collections of unicode characters.

The collection types have similar interfaces but 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.

As part of exploring the differences between the collection types, you’ll also consider performance: how quickly the collections can perform certain operations, such as adding to the collection or searching through it.

Section III: Building Your Own Types

Swift comes with basic building blocks, but its real power is in the custom things you can build to model parts of your app. Swift has no idea about playable characters and monsters and power-ups, for example — these are things you need to build yourself! You’ll learn how to do that in this section.

Now that you know the basics of Swift, it’s time to put everything you’ve learned together to create your own types.

You can create your own type by combining variables and functions into a new type definition. For example, integers and doubles might not be enough for your purposes, so you might need to create a type to store complex numbers. Or maybe storing first, middle and last names in three independent variables is getting difficult to manage, so you decide to create a FullName type.

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.

  • Chapter 10, Structures
  • Chapter 11, Properties
  • Chapter 12, Methods

Swift, in fact, includes four kinds of named types: structures, classes, enumerations and protocols. Now that you understand structures work with methods and properties you can see how the other named types use these same concepts, how they differ, and where you want to use each.

  • Chapter 13, Classes
  • Chapter 14, Advanced Classes
  • Chapter 15, Enumerations
  • Chapter 16, Protocols

Finally, you expand your knowledge of the type system by learning about generics: types and methods that take as input other types instead of just methods. Swift’s key to safety, speed and expressiveness lies in the ability to utilize generic types.

  • Chapter 17, Generics

Custom types make it possible to build large and complex things with the basic building blocks you’ve learned so far.

Chris Belanger

Contributors

Chris Belanger

Author

Over 300 content creators. Join our team.