Swift Apprentice Updated for Swift 4.2
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
forloop. You’ll also learn about
switchstatements 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
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.
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, Asynchronous 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 Value Semantics: 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:
Ehab Amer is a very enthusiastic Lead iOS developer with a very diverse experience, from building games to enterprise applications and POCs, especially when exploring new technologies. In his spare time, TV shows take the majority, followed by video games. When away from the screen, he goes with his friends for escape room experiences or to explore the underwater world through diving.
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 Beginning Swift Tutorials
To help celebrate the launch, we’re hard at work updating our free Getting Started with Swift tutorials, which are based directly on the content in the book. They’ll be out in just a few weeks — stay tuned to the site for updates!
Where to Go From Here?
Swift Apprentice, Fourth Edition is now 100% complete, fully updated for Swift 4.2 and is available today!
- If you’ve already bought the Swift Apprentice digital edition, 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 12 Launch Party, including over $9,000 in giveaways!
To be eligible for for this epic iOS 12 giveaway, all you have to do is leave a comment on the original launch post, letting us know which book or course is your favorite on this list — or which upcoming book or course you’re most excited about!
We hope you enjoy this update to one of our most-loved books. Stay tuned for more book releases and updates coming soon!