Welcome to the Swift Apprentice, fully updated for Xcode 12 and Swift 5.3!
In the last six 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 iOS, iPadOS, macOS, tvOS and watchOS. Swift is also being used as a server side technology on non-Apple platforms. That means what you learn in this book will be extremely useful as you expand your development skills and possibly work as a developer someday.
You’ll learn about basic things like constants, values, operations and types, and move up to more intermediate concepts like data structures, classes and enumerations. Finally, you’ll finish off by getting in-depth knowledge about protocol extensions, custom operators, protocol-oriented programming and generics. Swift lets you create beautiful abstractions to solve real-world problems that you will learn about in this book.
Swift is also a lot of fun! It’s easy to try out small snippets of code as you test new ideas. Programming is a hands-on experience, and Swift makes it fast and easy to both follow along with this book, as well as explore on your own.
Who this book is for
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.
If you want to get right into iOS app development while learning bits of the Swift language as you go, we recommend SwiftUI by Tutorials by the raywenderlich.com team. SwiftUI is so approachable that you may have even built a simple app or two with some sample code. As you go through this book, you may be wondering why you don’t see some of the syntaxes that appears in an elementary SwiftUI app. These include
@ObservedObject property wrappers, opaque return types and result builders. While property wrappers and opaque return types are eventually covered, they are advanced language features and require quite a bit of background to understand their inner workings. Using them, fortunately, is easy, and SwiftUI by Tutorials will show you how to do that.
How to use this book
Each chapter of this book presents some theory on the topic at hand, along with plenty of Swift code to demonstrate the practical applications of what you’re learning.
Since this is a book for beginners, we suggest reading it in order the first time. After that, the book will make a great reference for you to return to and refresh your memory on particular topics.
All the code in this book is platform-neutral; that means it isn’t specific to iOS, macOS or any other platform. The code runs in playgrounds, which you’ll learn about in the very first chapter.
As you read through the book, you can follow along and type the code into your own playground. That means you’ll be able to play with the code by making changes and see the results immediately.
You’ll find mini-exercises throughout the book, which are short exercises about the topic at hand. There are also challenges at the end of each chapter, which are either programming questions or longer coding exercises to test your knowledge. You’ll get the most out of this book if you follow along with these exercises and challenges.
What’s in store
This book is divided into four sections. Each section has a short introduction that describes its chapters, their topics and the overarching themes of the section. Here’s a brief overview of the book’s sections:
Section I: Swift Basics
The first section of the book starts at the very beginning of the computing environment: first, how computers work, and then, how Swift’s playgrounds feature works. With those logistics out of the way, you’ll take a tour of the fundamentals of the Swift language and learn the basics of managing data, structuring your code, performing simple operations and calculations, working with types.
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.
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.
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.
We would like to thank many people for their assistance in making this book possible:
- Janie Clayton For her previous work on the first, second and third editions of Swift Apprentice.
- Erik Kerber For his previous work on the first and second editions of Swift Apprentice.
- Our families: For bearing with us in this crazy time as we worked all hours of the night to get this book ready for publication!
- Everyone at Apple: For producing the amazing hardware and software we know and love, and for creating an exciting new programming language that we can use to make apps for that hardware!
- The Swift Community: For all the people, both inside and outside of Apple, who have worked very hard to make Swift the best computer language in the world.
- And most importantly, the readers of raywenderlich.com — especially you! Thank you so much for reading our site and purchasing this book. Your continued readership and support is what makes all of this possible!