This is a book for beginners of Apple’s modern programming language, Swift.
All the code in the book runs in easy-to-use Xcode playgrounds. This means you can focus on the core concepts of the Swift language, such as classes, protocols, and generics, instead of getting bogged down in the details of building programs.
This is a SwiftUI Apprentice tutorial; SwiftUI Apprentice focuses on building apps, while Swift Apprentice focuses on the Swift language itself.
This is a book for beginners of Apple’s modern programming language, Swift.
All the code in the book runs in easy-to-use Xcode playgrounds. This means you can focus on core Swift concepts like classes, protocols, and universals without getting bogged down in extraneous details.
This section covers some of the 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.
The chapters in this section will introduce you to the very basics of programming in Swift. From the basics of how computers work to language structures, you’ll cover enough of the language to be able to work with data and orchestrate the behavior of your code.
The chapter begins with some background to begin with. There will come a time when you have the basic data types in your head to do things with that data, and finally you’ll learn about the essential type of data, extras, that allow you to express potentially missing data.
These basics will get you Swiftly on your way, and before you know it, you’ll be ready for the more complex topics that follow. Let’s get started!
There you have it, your whirlwind introduction to the world of programming! You’ll start with an overview of computers and programming, then say hello to the playgrounds of Swift, where you’ll spend your time coding for the rest of this book. You will 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 will learn how to handle different types, including strings, which allow you to represent text. You’ll learn about conversion between types and get an introduction to type inference, making your life as a programmer easier. You will learn about tuple types, which allow you to group values of any type together.
You’ll learn how to make decisions and repeat tasks in your programs using syntax for flow control. You’ll also learn about booleans, which represent true and false values, and how you can use them to compare data.
Continuing on the topic of code that doesn’t run 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.
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 blocks.
This chapter covers options, a special type in Swift that represents a value, or the lack of one. By the end of this chapter, you’ll know why you need extra tools and how to use them safely.
Until now, you’ve mostly seen data as individual elements. Although tuples can have multiple pieces of data, you must specify the size in advance; a three-string tuple is a completely different type of two-string tuple, and the conversion between the two is not trivial. In this section you will learn about collection types in Swift. Collections are flexible “containers” that allow you to store any number of values together.
There are several types of collections in Swift, but three important ones are arrays, dictionaries, and sets. You will learn how to apply custom operations and iterate over collection types. Finally, you will return to strings, which are sets of characters.
All collection types have similar interfaces but very different use cases. As you read these sections, keep the differences in mind and you’ll begin to understand which type to use and when.
Arrays are the most common type of collection you’ll encounter in Swift, which store an ordered list of elements of the same type. Dictionaries, on the other hand, allow you to efficiently search for items using a key. Finally, sets support an unordered collection of unique elements. You will learn all about these three types in this chapter.
If you have collections of items, you’ll want to perform operations on them. For example, sort them, filter them, stack them, etc. Swift gives you a powerful language construct, closures, that allow you to endlessly customize the behavior of such operations. In this chapter, you’ll learn about the most common Swift collection algorithms and customize them with closures.
Text processing is an essential addition to any computer language, and String is Swift’s powerful type for text processing. Strings are bidirectional sets of character types that balance correctness, performance, and ease of use.
Finding patterns in text is a common task you will encounter while programming. Swift provides a power type called Regex to handle this task. Using standard syntax, you can express complex matching patterns to extract information from text. You can use the all-new regular expression constructor syntax for improved compile-time support, which increases clarity and readability.
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 specified types. Frameworks 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: structs, classes, enumerations, and protocols. Here, you’ll learn how the other named types use the concepts of methods and properties, how they differ, and where you might want to use each.
You’ll also learn about protocols and generics, which are types and methods that take other types as input rather than just methods, and custom types to create big and complex things!
The standard library has many useful types such as Int, Double and String. But, unfortunately, it does not include pizza. Structs are types that can store named properties and define actions and behaviors. In this chapter, you’ll define your own structure types and start building a pizza empire.
In this chapter, you’ll learn about stored and computed properties, as well as some tricks, such as how to watch for changes in a property’s value and defer the initialization of a stored property.
Methods are simply functions inside a structure. You’ll take a close look at how methods and initializers help you create fully functional custom types.
Structures allow you to define your own named types with their own properties and methods. In this chapter, you’ll learn about classes that are very similar to structs, but have important differences that make them an invaluable addition to your toolbox.
This chapter continues with class types, which describe how Swift supports the traditional concepts of inheritance and polymorphism. You’ll also learn about two-phase class initialization, which you’ll need to create a proper class hierarchy. This discussion will lay the groundwork for using these concepts with Swift value types.
In this chapter, you learn about enumerations, a type that groups together related, mutually exclusive register values. You’ll also learn about unpopulated types and finally learn what’s under the hood.
Protocols are a type that can unify common behavior between structs, classes, and enumerations by defining an interface or pattern for the actual concrete type. Protocols provide polymorphism for all types and overcome the limitation of single inheritance that you saw with classes.
In this chapter, you’ll learn what generic types are, how to write generic code, and step back and look at generic types in Swift—dictionaries, arrays, and extras—from this new perspective.