Table of Contents (the real thing)

  1. Chapter 1

    Swift is a programming language you can rely on. It’s a programming language you can take home to meet your family. Safe, reliable, speedy, friendly, easy to talk to. And while Swift is best known for being the programming language of choice for Apple’s platforms, such as iOS, macOS, watchOS, and tvOS, the open source Swift project also runs on Linux and Windows, and is gaining ground as a systems programming language, as well as on the server. You can build everything from mobile apps, to games, to web apps, to frameworks and beyond. Let’s get started!

    1. “Swift is a language for everything”

    2. “The swift evolution of Swift”

    3. “Swift into the future”

    4. “How you’re going to write Swift”

    5. “The path you’ll be taking”

    6. “Getting Playgrounds”

    7. “Creating a Playground”

    8. Using a Playground to code Swift

    9. “Basic building blocks”

    10. “A Swift example”

    11. “Congrats on your first steps with Swift!”

  2. Chapter 2

    You already know the gist of Swift. But it’s time to dive into the building blocks in more detail. You’ve seen enough Swift to be dangerous, and it’s time to put it into practice. You’ll use Playgrounds to write some code, using statements, expressions, variables, and constants—the fundamental building blocks of Swift. In this chapter, you’ll build the foundation for your future Swift programming career. You’ll get to grips with Swift’s type system, and learn the basics of using strings to represent text. Let’s get going...and you can see for yourself how swiftly you can write Swift code.

    1. “Building from the blocks”

    2. “Basic operators”

    3. “Operating swiftly with mathematics”

    4. “Expressing yourself”

    5. “Names and types: peas in a pod”

    6. “Not all data is numbers”

    7. Stringing things along with types

    8. “String interpolation”

  3. Chapter 3

    You already know about expressions, operators, variables, constants, and types in Swift. It’s time to consolidate and build on that knowledge and explore some more advanced Swift data structures and operators: collections and control flow. In this chapter, we’re going to talk about putting collections of data into variables and constants, and how to structure data, manipulate data, and operate on data using control flow statements. We’ll be looking at other ways to collect and structure data later in the book, but for now let’s get started with arrays, sets, and dictionaries.

    1. “Sorting pizzas”

    2. “Swift collection types”

    3. “Collecting values in an array

    4. “How big is that array, exactly? Is it empty?”

    5. “Collecting values in a set

    6. “Collecting values in a dictionary

    7. “Tuples”

    8. “Everyone needs a good alias

    9. “Control flow statements”

    10. “if statements”

    11. “switch statements”

    12. “Building a switch statement”

    13. “Range operators”

    14. “More complex switch statements”

    15. “Getting repetitive with loops”

    16. “Building a for loop”

    17. “Building a while loop”

    18. “Building a repeat-while loop”

    19. “Solving the pizza-sorting problem”

    20. “Phew, that’s a lot of Swift!”

  4. Chapter 4

    Functions in Swift allow you to package up a specific behavior or unit of work into a single block of code that you can call from other parts of your program. Functions can be standalone, or they can be defined as part of a class, a structure, or an enumeration, where they are usually referred to as methods. Functions let you break down complex tasks into smaller, more manageable, and more testable units. They’re a core part of the way you structure a program with Swift.

    1. “Functions in Swift let you reuse code

    2. Built-in functions”

    3. “What can we learn from built-in functions?”

    4. “Improving the situation with a function”

    5. “Writing the body of the function”

    6. “Using functions”

    7. “Functions deal in values”

    8. “Many happy returns (from your functions)”

    9. “A variable number of parameters”

    10. “What can you pass to a function?”

    11. “Every function has a type”

    12. “Function types as parameter types”

    13. Multiple return types”

    14. “Functions don’t have to stand alone”

    15. “Switching with enums”

  5. Chapter 5

    Functions are useful, but sometimes you need more flexibility. Swift allows you to use a function as a type, just like an integer or string. This means you can create a function and assign it to a variable. Once it’s assigned to a variable, you can call the function using the variable, or pass the function to other functions as a parameter. When you create and use a function in this manner, it’s called a closure. Closures are useful because they can capture references to constants and variables from the context in which they are defined. This is called closing over a value, hence the name.

    1. “Meet the humble closure”

    2. “Closures are better with parameters”

    3. “Boiling it all down to something useful”

    4. “Reducing with closures”

    5. “Capturing values from the enclosing scope”

    6. “Escaping closures: the contrived example

    7. “Autoclosures provide flexibility”

    8. “Shorthand argument names”

  6. Chapter 6

    Working with data often involves defining your own kinds of data. Structures—often shortened to their Swift language keyword, structs—allow you to create your own custom data types (just like String and Int are data types) by combining other types. Using structs to represent the data that your Swift code is working with allows you to step back and consider how the data that flows through your code fits together. Structs can hold variables and constants (which are called properties when they’re inside a struct) and functions (which are called methods). Let’s add some structure to your world, and dive into structs.

    1. “Let’s make a pizza, in all its glory...”

    2. “The initializer behaves just like a function

    3. “Static properties make structures more flexible”

    4. “Methods inside structures”

    5. “Changing properties using methods”

    6. “Computed properties”

    7. Getters and setters for computed properties”

    8. “Implementing a setter”

    9. “Swift Strings are actually structs”

    10. “The case for lazy properties”

    11. “Using lazy properties”

  7. Chapter 7

    Structs showed us how useful it can be to build custom types. But Swift has a number of other tricks up its sleeve, including classes. Classes are similar to structs: they let you make new data types that have properties and methods within them. However, in addition to being reference types—instances of a specific class share a single copy of their data (unlike structs, which are value types and get copied)—classes support inheritance. Inheritance allows one class to build upon the features of another.

    1. “A struct by any other name (that name: a class)”

    2. “Inheritance and classes”

    3. Replacing Overriding methods”

    4. “Final classes”

    5. “Automatic reference counting”

    6. “Mutability”

  8. Chapter 8

    You know all about classes and inheritance, but Swift has a few more tricks for structuring programs, and they’re a lot Swiftier. Meet protocols and extensions. Protocols in Swift let you define a blueprint specifying methods and properties that are required for some purpose, or some piece of functionality. A protocol is adopted by a class, structure, or enumeration, and the actual implementation happens there. Types that provide the functionality needed, and adopt a protocol, are referred to as conforming to that protocol. Extensions simply let you add new functionality to existing types.

    1. The Robot Factory

    2. “Protocol inheritance”

    3. “Mutating methods”

    4. “Protocol types and collections”

    5. “Computed properties in extensions”

    6. “Extending a protocol”

    7. “Useful protocols and you”

    8. “Conforming to Swift’s protocols”

  9. Chapter 9

    Dealing with data that doesn’t exist can be challenging. Thankfully, Swift has a solution. Meet optionals. In Swift, an optional allows you to work with a value, or with the absence of a value. They’re one of the many ways that Swift is designed to be a safe language. You’ve occasionally seen optionals in your code so far, and now we’re going to explore them in more depth. Optionals make Swift safe because they keep you from accidentally writing code that would break if it’s missing data, or if something can sometimes return a value that isn’t actually a value.

    1. “Dealing with something that’s missing”

    2. “Why you might need an optional”

    3. “Optionals and handling missing data”

    4. Images Unwrapping optionals Images

    5. Images Unwrapping optionals with guard Images

    6. “Force unwrapping”

    7. “Generics”

    8. “A queue with generics”

    9. “Here’s our new Queue type”

  10. Chapter 10

    It’s time to use your toolbox full of Swift techniques, features, and components: you’re going to start building user interfaces. A Swift UI, if you will. We’re going to bring everything together in this chapter to create our first true user interface. We’ll build a whole experience using SwiftUI, the user interface framework for Apple’s platforms. We’ll still be using Playgrounds, at least initially, but everything we’re doing here will lay the groundwork for an actual iOS application. Get ready: this chapter is full of code and a lot of new concepts.

    1. “What’s a UI framework, anyway?”

    2. “Your first SwiftUI UI”

    3. “UI building blocks”

    4. “Making a list, checking it…quite a few times, to get it perfect”

    5. “User interfaces with state”

    6. “Buttons are for pressing”

    7. “Let’s see how far you’ve come”

    8. Images Customizing your views with modifiers Images

    9. “Create a new SwiftUI Xcode project, for iOS”

    10. “Your Xcode will look something like this”

    11. “Create a new type to store a todo item in”

    12. “Make sure each todo item can be uniquely identified”

    13. “Create a user interface for the app”

    14. “Implement a way to save the list of todos”

    15. “So, that’s a UI framework?”

  11. Chapter 11

    SwiftUI lets you do more than work with Buttons and Lists. You can also use shapes, animations, and more! In this chapter, we’re going to look at some of the more advanced ways you can construct a SwiftUI UI, and connect it to a data source that isn’t just user-generated content like todo items. SwiftUI lets you build responsive UIs that handle events coming from all sorts of places. We’ll be working with Xcode, Apple’s IDE, and we’ll be focusing on iOS apps, but everything you’ll learn is applicable to SwiftUI on iPadOS, macOS, watchOS, and tvOS as well. It’s time to explore the depths of SwiftUI!

    1. “What fancy things can be done with a UI framework?”

    2. “Create a new SwiftUI Xcode project, for iOS”

    3. “The Executive Timer UI and features”

    4. “Creating the basic elements of the app”

    5. “Pieces of the UI”

    6. “Setting up the UI of the Executive Timer”

    7. “Coding the pieces of the UI”

    8. “Combining the three elements”

    9. “The finishing touches”

    10. “Tabbed views for a clean UI”

    11. “2 Build a TabView containing your views”

    12. “Creating a new tabbed ContentView”

    13. “Creating the tabs and the TabView”

    14. “Running your new tabbed Executive Timer”

  12. Chapter 12

    You’ve done lots of Swift. You’ve used Playgrounds and Xcode. We knew you’d have to say goodbye eventually, but the time has finally arrived. It’s hard to part, but we know you’ve got what it takes. In this chapter, our final chapter together (at least in this book), we’re going to do a final pass through many of the concepts you’ve learned, and build a handful of things together. We’ll make sure your Swift is shipshape, and give you some pointers on what to do next. Some homework, if you will. It’ll be fun, and we’ll part on a high note.

    1. “A journey must end...”

    2. A recipe for a welcome screen

    3. “Step by step assembly of the welcome screen”

    4. Smash Share the state”

    5. “It’s time for our old friend...”

    6. “Building an app with multiple views that share state”

    7. “Building a two-view score tracker”

    8. “The ObservableObject”

    9. “The first view”

    10. “The second view”

    11. “The first view, again”

    12. “A fancy AsyncImage”

    13. “Meet Vapor, the Swift web framework”

    14. “Sending data over the web with Vapor”

Get Head First Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.