Table of Contents (the real thing)
-
Your brain on Swift. Here you are trying to learn something, while here your brain is doing you a favor by making sure the learning doesn’t stick. Your brain’s thinking, “Better leave room for more important things, like which wild animals to avoid and whether naked snowboarding is a bad idea.” So how do you trick your brain into thinking that your life depends on knowing Swift?
-
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!
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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!
-
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.
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.