Written by Ray Fix
Welcome to the Swift Apprentice: Fundamentals, fully updated for Xcode 14.2 and Swift 5.7! This book was derived from the first three sections of the Swift Apprentice, Seventh Edition. It expands on the core concepts the are vital to a solid understanding of the Swift language.
In the last eight years, Swift has gone from 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 progressive, industrial-strength nature, Swift is an excellent choice for the beginning programmer since Xcode offers a sandbox-type environment where you can directly execute Swift statements to try out various language features without creating a whole app first.
Developers worldwide use Swift to build thousands of amazing apps for iOS, iPadOS, macOS, tvOS and watchOS. Swift is also in 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 follow along with this book and explore on your own.
Who This Book Is For
If you’re a complete beginner at 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 appear 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 theory and 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 an excellent reference for you to return to and refresh your memory on particular topics.
All the code in this book is platform-neutral; 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 a playground. That means you’ll be able to experiment with the code by making changes and see the results immediately.
Throughout the book, you’ll find mini-exercises, which are short exercises about the topic at hand. At the end of each chapter, there are also challenges. These are either programming questions or more extended 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 consists of three 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 book’s first section starts at the 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 — these are things you need to build yourself! You’ll see how to organize your types with protocols and learn how to use generics to make your code more reusable.