iOS & Swift

Swift Apprentice: Fundamentals

Beginning programming with Swift! This book covers the fundamentals of Swift: Apple’s modern programming language for iOS, iPadOS, macOS, watchOS and beyond. By Ehab Amer, Matt Galloway, Alexis Gallagher & Eli Ganim.

Read for Free with the Personal Plan* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99 $29.99* *Includes access to all of our online reading features.
Leave a rating/review
Download materials
Buy paperback—Amazon Comments
Save for later
Share

Who is this for?

This is a book for complete beginners to Apple’s modern programming language — Swift.

Covered concepts

All the code in the book works inside of Xcode’s easy-to-use playgrounds. That means you can focus on core Swift language concepts, such as classes, protocols, and generics, instead of getting bogged down in the details of building apps.

This is a companion book to the SwiftUI Apprentice; the SwiftUI Apprentice focuses on building apps, while Swift Apprentice focuses on the Swift language itself.

This is a book for complete beginners to Apple’s modern programming language — Swift.

All the code in the book works inside of Xcode’s easy-to-use playgrounds. That means you can focus on core Swift language concepts, such as classes, protocols, and generics without getting bogged down by extraneous details.

This...

more

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book and more.

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 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. Once you have the basic data types in your head, it’ll be time to do things with that data, and finally, you’ll learn about an essential data type, optionals, that let you express potentially missing data.

These fundamentals will get you Swiftly on your way, and before you know it, you’ll be ready for the more advanced topics that follow. Let’s get started!

1
Toggle description
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, 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.
Toggle description
You’ll learn about handling different types, including strings that allow you to represent text. You’ll learn about converting between types and get an introduction to type inference, which simplifies your life as a programmer. You’ll learn about tuple types which allow you to group values of any type together.
Toggle description
You’ll learn how to make decisions and repeat tasks in your programs 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.
Toggle description
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 that are particularly powerful in Swift.
Toggle description
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.
Toggle description
This chapter covers optionals, a special type in Swift representing either a 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

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 upfront; 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 to apply custom operations and loop over collection types. Finally, you’ll revisit strings, which are collections of characters.

All the collection types share similar interfaces but have 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.

Toggle description
Arrays are the most common collection type you’ll run into in Swift that keep an ordered list of elements of the same type. On the other hand, Dictionaries let you look up elements efficiently using a key. Finally, Sets maintain an unordered collection of unique elements. You’ll learn all about these three types in this chapter.
Once you have collections of items, you will want to perform operations with them. For example, sort them, filter them, add them up, etc. Swift gives you a powerful language construct, the closure, that lets you infinitely customize the behavior of such operations. In this chapter, you will learn about Swift’s most common collection algorithms and customize them with closures.
Toggle description
Text processing is an essential application for any computer language, and String is Swift’s powerhouse type for text handling. Strings are bi-directional collections of Character types that balance correctness, performance and ease of use.
Toggle description
Searching for patterns in text is a common task you'll encounter in your programming travels. Swift provides a power type called Regex to perform that task. Using standard syntax, you can express complicated matching patterns to extract information from text. You can use an all-new regex builder syntax for improved compile-time support, which maximizes clarity and readability.

Section III: Building Your Own Types

You can create your own type by combining variables and functions into a new type definition. 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.

Swift includes four kinds of named types: structures, classes, enumerations and protocols. You’ll learn here how other named types use the concepts of methods and properties, how they differ, and where you want to use each.

You’ll also learn about protocols & generics, which are types and methods that take as input other types instead of just methods, as well as custom types to build larger and complex things!

Toggle description
The standard library has many useful types like Int, Double and String. However, it sadly does not include a Pizza type. Structures are types that can store named properties and define actions and behaviors. In this chapter, you will define your custom structure types and begin building a Pizza empire.
Toggle description
In this chapter, you’ll learn about stored and computed properties, along with some tricks, such as how to monitor changes in a property’s value and delay the initialization of a stored property.
Toggle description
Methods are merely functions that reside in a structure. You’ll look closely at how methods and initializers help you build full-featured, custom types.
Toggle description
Structures let you define your own named types with custom properties and methods. In this chapter, you’ll get acquainted with classes, which are much like structures but have important differences that make them an invaluable addition to your toolbox.
Toggle description
This chapter continues with class types describing how Swift supports the traditional concepts of inheritance and polymorphism. You will also learn about two-phase class initialization that you will need to build proper class hierarchies. This discussion will lay the foundation for using these concepts with Swift’s value types.
Toggle description
In this chapter, you’ll learn about enumerations, a type that groups related, mutually exclusive case values. You’ll also learn about uninhabited types and finally discover what an optional is under the hood.
Toggle description
Protocols are a type that can bridge common behaviors between structs, classes, and enums by defining an interface or template for an actual concrete type. Protocols enable polymorphism across all types and overcome the single inheritance limitation you saw with classes.
Toggle description
In this chapter, you’ll learn what generics are, how to write generic code, and loop back and look at the generic types in Swift - dictionaries, arrays, and optionals - from this new perspective.