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 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.
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.
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.
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.
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.
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.
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!
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.
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.
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.
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.
Ray Fix works on next-generation microscopes made for iPad at [Discover Echo Inc](https://discover-echo.com). Ray enjoys...
Who is this book for
This is a book for complete beginners to Apple’s new, modern programming language — Swift.
Concepts covered in this book
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.
Sixth Edition · iOS 14, Swift 5.3, Xcode 12
v6.0 · Oct 1 2020 · Cosmin Pupăză, Matt Galloway, Ben Morrow, Alexis Gallagher, Ehab Yosry Amer & Eli Ganim