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.
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 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...
moreSection 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!
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 notationBig-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.
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!
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.