iOS & Swift

Swift Apprentice

Beginning programming with Swift! This book takes you from beginner to advanced in Swift: Apple’s modern programming language for iOS. By Cosmin Pupăză, Ehab Amer, Eli Ganim, Matt Galloway, Alexis Gallagher & Ben Morrow.

Read for Free with the Personal Plan* * Includes this and all other books in our online library See all benefits
Buy Individually $59.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 new, 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 iOS Apprentice; the iOS Apprentice focuses on building apps, while Swift Apprentice focuses on the Swift language itself.

This book is for complete beginners to Swift and coding. No prior programming experience necessary!

This is a book for complete beginners to Apple’s new, 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...

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 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, then 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 a very important data type. 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!

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.
Toggle description
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.
Toggle description
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.
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 which 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 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

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 to apply custom operations and loop over collection types. Finally, you’ll revisit strings.

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.

The usual way to talk about performance is with big-O notation. If you’re not familiar with it already, read on for a brief introduction.

Introducing big-O notation

Big-O notation is a way to describe running time, or how long an operation takes to complete. The idea is that the exact time an operation takes isn’t important; it’s the relative difference in scale that matters.

Imagine you have a list of names in some random order, and you have to look up the first name on the list. It doesn’t matter whether the list has a single name or a million names — glancing at the very first name always takes the same amount of time. That’s an example of a constant time operation, or O(1) in big-O notation.

Now say you have to find a particular name on the list. You need to scan through the list and look at every single name until you either find a match or reach the end. Again, we’re not concerned with the exact amount of time this takes, just the relative time compared to other operations.

To figure out the running time, think in terms of units of work. You need to look at every name, so consider there to be one “unit” of work per name. If you had 100 names, that’s 100 units of work. What if you double the number of names to 200? How does that change the amount of work? The answer is it also doubles the amount of work. Similarly, if you quadruple the number of names, that quadruples the amount of work.

This is an example of a linear time operation, or O(N) in big-O notation. The size of the input is the variable N, which means the amount of time the operation takes is also N. There’s a direct, linear relationship between the input size (the number of names in the list) and the time it will take to search for one name.

You can see why constant time operations have the number 1 in O(1). They’re just a single unit of work, no matter what!

You can read more about big-O notation by searching the Web. You’ll only need constant time and linear time in this book, but there are other such time complexities out there.

Big-O notation is particularly important when dealing with collection types, because collections can store very large amounts of data, and you need to be aware of running times when you add, delete or edit values.

For example, if collection type A has constant-time searching and collection type B has linear-time searching, which you choose to use will depend on how much searching you’re planning to do.

Toggle description
Arrays are the most common collection type you’ll run into in Swift. Arrays are typed, as are regular variables and constants, and store multiple values like a single list.
Swift has an object you can use to break up code into reusable chunks: a closure. These are particularly important when dealing with collections.
Toggle description
Finally, you will revisit strings, which are actually bi-directional collections of unicode characters.

Section III: Building 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.

Swift, in fact, includes four kinds of named types: structures, classes, enumerations and protocols. Now that you understand how 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.

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. It’s time to take your Swift apprenticeship to the next level!

Toggle description
This chapter introduces structures, which are the first named type you’ll learn about. Structures are types that can store named properties and define their own behaviors.
Toggle description
In this chapter, you’ll learn about stored and computed properties, along with some tricks to deal with properties, how to monitor changes in a property’s value and delay initialization of a stored property.
Toggle description
Methods are merely functions that reside in a structure. In this chapter, you’ll take a closer look at methods and initializers.
Toggle description
Structures introduced you to your first named type, which lets you define your own named types. In this chapter, you’ll get acquainted with classes, which are much like structures.
Toggle description
This chapter introduces the finer points of classes and helps you understand how you can create, use and manage complex classes.
Toggle description
In this chapter, you’ll learn how enumerations work and when they’re useful. As a bonus, you’ll 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.
Toggle description
In this chapter, you’ll learn what generics are, learn how to write your own generic code, and loop back and look at the generic types in Swift - dictionaries, arrays, and optionals - from this new perspective.

Section IV: Advanced Topics

You’ve made it to the final section of this book! In this section, you’ll delve into some important but more advanced topics to round out your Swift apprenticeship.

Swift gives you powerful tools for hiding complexity and organizing your code into easier to digest units. This chapter details how to do that.
You’ll learn how you can define your own operators and subscripts to make your types feel even more like built-in language constructs.
Toggle description
With pattern matching you can accomplish more — with less typing. You’ll master all of its many forms in this chapter.
Toggle description
In the real world, some errors cannot be avoided. Handling them gracefully is what sets apart mediocre code from great code.
Toggle description
You will learn about the type serialization system introduced in Swift 4 with particular emphasis on the JSON format.
Toggle description
This chapter digs into the details of Swift memory management examining the relation between objects. It shows you how you avoid common leaks.
Toggle description
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.
Toggle description
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.
Toggle description
Learn how to use constraints to make generic code more useful and how to hide implementation details with opaque return types.