Swift Apprentice Updated for Swift 4

Chris Belanger

Happy Monday, everyone — it’s time for another Swift 4 book release as part of our iOS 11 Launch Party!

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

For the new third edition, we’ve made some major changes:

  • New chapter: Strings. We’ve addd an entirely new chapter showcasing the new approach for Strings in Swift 4.
  • New chapter: Codable. We’ve also added a new chapter on Codable and serialization.
  • Extended chapter: Operators and Subscripts. We’ve greatly extended the chapter on custom operators and subscripts to covers keypaths in detail.
  • More Swift 4 updates! We’ve also made many other changes to upgrade the book for Swift 4 throughout the book!

This will be a free update for existing Swift Apprentice PDF customers — our way of saying “thanks” to all of our readers for their support!

Don’t own the Swift Apprentice yet? Read on to see how you can get a copy — and take advantage of our iOS 11 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 some programming practice and test your knowledge along the way.

In a little over three 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 Float Control: 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.

Section IV: Advanced Topics

The final section of the book covers more advanced topics in Swift. You’ll learn about specific things, such as how to handle problems that come up as your code runs, as well as about more general things such as memory management, which will help you understand some of Swift’s behind-the-scenes mechanisms.

In this section, you’ll delve into some important but more advanced topics to round out your Swift apprenticeship:

  • Chapter 18, Access Control and Code Organization: Swift gives you powerful tools for hiding complexity and organizing your code into easier to digest units. This chapter details how to do that.
  • Chapter 19, Custom Operators, Subscripts, Keypaths: You’ll learn how you can define your own operators and subscripts to make your types feel even more like built-in language constructs. You will also learn about type-safe keypaths introduced in Swift 4.
  • Chapter 20, Pattern Matching: With pattern matching you can accomplish more — with less typing. You’ll master all of its many forms in this chapter.
  • Chapter 21, Error Handling: In the real world, some errors cannot be avoided. Handling them gracefully is what sets apart mediocre code from great code.
  • Chapter 22, Encoding and Decoding Types: You will learn about the type serialization system introduced in Swift 4 with particular emphasis on the JSON format.
  • Chapter 23, Async Closures and Memory Management: This chapter digs into the details of Swift memory management examining the relation between objects. It shows you how you avoid common leaks.
  • Chapter 24, Value Types and Reference Types: 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.
  • Chapter 25, Protocol-Oriented Programming: 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.

About the Authors

Of course, our book would be nothing without our team of experienced and dedicated authors:

Janie Clayton is an independent iOS developer. She spent a year apprenticed to a super genius programming robots and learning the forgotten ways of long term software maintenance and development. Janie is the coauthor on several books on iOS and Swift development. Janie lives outside of Madison, Wisconsin with her attempted grumble of pugs and multitude of programming books. Janie writes her various musings on her blog at RedQueenCoder.com.

Alexis Gallagher is a software engineer who is always looking for the conceptual deep dive and always hoping to find pearls down at the bottom. When he’s not coding, he’s out and about in sunny San Francisco.

Matt Galloway is a software engineer with a passion for excellence. He stumbled into iOS programming when it first was a thing, and has never looked back. When not coding, he likes to brew his own beer.

Eli Ganim is an engineering manager at Facebook. He is passionate about teaching, writing, and sharing his knowledge with others.

Ben Morrow delights in discovering the unspoken nature of the world. He’ll tell you the surprising bits while on a walk. He produces beauty by drawing out the raw wisdom that exists within each of us.

Cosmin Pupăză is a software developer and tutorial writer from Romania. He has worked with more than a dozen programming languages over the years, but none of them has made such a great impact on himself as the advent of Swift. When not coding, he either plays the guitar or studies WWII history. Cosmin blogs about Swift at cosminpupaza.wordpress.com.

Steven Van Impe is a computer science lecturer at the University College of Ghent, Belgium. When he’s not teaching, Steven can be found on his bike, rattling over cobblestones and sweating up hills, or relaxing around the table, enjoying board games with friends. You can find Steven on Twitter as @svanimpe.

Free Swift Apprentice Chapter

To help celebrate the launch, we’re going to open up the book and share a free chapter with you this week! This will give you a chance to check out some of the new pieces in the book. Stay tuned!

Now Available in ePub!

And as another exciting announcement, by popular request, the Swift Apprentice is now available in ePub format. Take it on the go with you on your iPad, iPhone or other digital reader and enjoy all the mobile reading benefits that ePub has to offer!

For the first time, Swift Apprentice is available in ePub format too!

Where to Go From Here?

Swift Apprentice, Third Edition is now 100% complete, fully updated for Swift 4 and is available today!

  • If you’ve already bought the Swift Apprentice PDF, you can log in to your account and download the new book in PDF and ePub format immediately on our store page.
  • If you don’t have the Swift Apprentice yet, you can grab your own very own copy in our online store.

And to help sweeten the deal, the digital edition of the book is on sale for $49.99! But don’t wait — this sale price is only available for a limited time.

Speaking of sweet deals, be sure to check out the great prizes we’re giving away this year with the iOS 11 Launch Party, including over $9,000 in giveaways!

To enter, simply retweet this post using the #ios11launchparty hashtag by using the button below:

We hope you enjoy this update to one of our most-loved books. Stay tuned for more book releases and updates coming soon!

Chris Belanger

Chris Belanger is the Book Team Lead and Lead Editor for raywenderlich.com. If there are words to wrangle or a paragraph to ponder, he‘s on the case.

In the programming world, Chris has over 20 years of experience with multiple database platforms, real-time industrial control systems, and enterprise healthcare information systems.

When he kicks back, you can usually find Chris with guitar in hand, looking for the nearest beach, or exploring the lakes and rivers in his part of the world in a canoe.

Other Items of Interest

Save time.
Learn more with our video courses.

raywenderlich.com Weekly

Sign up to receive the latest tutorials from raywenderlich.com each week, and receive a free epic-length tutorial as a bonus!

Advertise with Us!

PragmaConf 2016 Come check out Alt U

Our Books

Our Team

Video Team

... 27 total!

iOS Team

... 83 total!

Android Team

... 47 total!

Unity Team

... 16 total!

Articles Team

... 4 total!

Resident Authors Team

... 32 total!

Podcast Team

... 4 total!

Recruitment Team

... 8 total!

Illustration Team

... 4 total!