Intro 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? |
|
|
|
|
xxii | |
|
We know what you're thinking |
|
|
xxiii | |
|
We know what your brain is thinking |
|
|
xxiii | |
|
Metacognition: thinking about thinking |
|
|
xxv | |
|
|
xxvi | |
|
Here's what you can do to bend your brain into submission |
|
|
xxvii | |
|
|
xxviii | |
|
The technical review team |
|
|
xxx | |
|
|
xxxi | |
1 Introducing Swift: Apps, Systems, and Beyond! |
|
|
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. |
|
|
|
|
|
Swift is a language for everything |
|
|
2 | (2) |
|
The swift evolution of Swift |
|
|
4 | (1) |
|
|
5 | (1) |
|
How you're going to write Swift |
|
|
6 | (2) |
|
The path you'll be taking |
|
|
8 | (1) |
|
|
9 | (2) |
|
|
11 | (1) |
|
Using a Playground to code Swift |
|
|
12 | (2) |
|
|
14 | (6) |
|
|
20 | (5) |
|
Congrats on your first steps with Swift! |
|
|
25 | (5) |
2 Swift by Name: Swift by Nature |
|
|
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. |
|
|
|
|
30 | (1) |
|
|
31 | (1) |
|
Operating swiftly with mathematics |
|
|
32 | (1) |
|
|
33 | (3) |
|
Names and types: peas in a pod |
|
|
36 | (4) |
|
|
40 | (2) |
|
Stringing things along with types |
|
|
42 | (6) |
|
|
48 | (8) |
3 Collecting and controlling: Going Loopy for Data |
|
|
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. |
|
|
|
|
56 | (1) |
|
|
57 | (1) |
|
Collecting values in an array |
|
|
58 | (2) |
|
How big is that array, exactly? Is it empty? |
|
|
60 | (1) |
|
Collecting values in a set |
|
|
61 | (2) |
|
Collecting values in a dictionary |
|
|
63 | (2) |
|
|
65 | (1) |
|
Everyone needs a good alias |
|
|
66 | (2) |
|
|
68 | (1) |
|
|
69 | (1) |
|
|
70 | (4) |
|
Building a switch statement |
|
|
74 | (2) |
|
|
76 | (1) |
|
More complex switch statements |
|
|
77 | (1) |
|
Getting repetitive with loops |
|
|
78 | (1) |
|
|
79 | (3) |
|
|
82 | (1) |
|
Building a repeat-while loop |
|
|
83 | (1) |
|
Solving the pizza-sorting problem |
|
|
84 | (2) |
|
Phew, that's a lot of Swift! |
|
|
86 | (13) |
4 Functions and Enums: Reusing Code on Demand |
|
|
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 in Swift let you reuse code |
|
|
99 | (1) |
|
|
100 | (1) |
|
What can we learn from built-in functions? |
|
|
101 | (3) |
|
Improving the situation with a function |
|
|
104 | (1) |
|
Writing the body of the function |
|
|
105 | (1) |
|
|
106 | (2) |
|
|
108 | (2) |
|
Many happy returns (from your functions) |
|
|
110 | (2) |
|
A variable number of parameters |
|
|
112 | (3) |
|
What can you pass to a function? |
|
|
115 | (1) |
|
Every function has a type |
|
|
116 | (4) |
|
Function types as parameter types |
|
|
120 | (2) |
|
|
122 | (2) |
|
Functions don't have to stand alone |
|
|
124 | (3) |
|
|
127 | (7) |
5 Closures: Fancy, Flexible Functions |
|
|
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. |
|
|
|
|
134 | (2) |
|
Closures are better with parameters |
|
|
136 | (8) |
|
Boiling it all down to something useful |
|
|
144 | (1) |
|
|
145 | (3) |
|
Capturing values from the enclosing scope |
|
|
148 | (2) |
|
Escaping closures: the contrived example |
|
|
150 | (3) |
|
Autoclosures provide flexibility |
|
|
153 | (3) |
|
|
156 | (7) |
6 Structures, properties, and methods: Custom Types and Beyond |
|
|
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. |
|
|
|
Let's make a pizza, in all its glory |
|
|
163 | (6) |
|
The initializer behaves just like a function |
|
|
169 | (3) |
|
Static properties make structures more flexible |
|
|
172 | (4) |
|
Methods inside structures |
|
|
176 | (1) |
|
Changing properties using methods |
|
|
177 | (2) |
|
|
179 | (4) |
|
Getters and setters for computed properties |
|
|
183 | (1) |
|
|
184 | (2) |
|
Swift Strings are actually structs |
|
|
186 | (1) |
|
The case for lazy properties |
|
|
187 | (1) |
|
|
188 | (6) |
7 Classes, actors, and inheritance: Inheritance is Always a Thing |
|
|
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. |
|
|
|
A struct by any other name (that name: a class) |
|
|
194 | (2) |
|
|
196 | (4) |
|
|
200 | (4) |
|
|
204 | (4) |
|
Automatic reference counting |
|
|
208 | (1) |
|
|
209 | (13) |
8 Protocols and extensions: A Swift Lesson in Protocol |
|
|
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. |
|
|
|
|
222 | (5) |
|
|
227 | (2) |
|
|
229 | (3) |
|
Protocol types and collections |
|
|
232 | (4) |
|
Computed properties in extensions |
|
|
236 | (4) |
|
|
240 | (1) |
|
|
241 | (4) |
|
Conforming to Swift's protocols |
|
|
245 | (10) |
9 Optionals, unwrapping, generics, and more: Nothing Optional About It |
|
|
Dealing with data that doesn't exist can be challenging. |
|
|
|
Thankfully, Swift has a solution. |
|
|
|
|
|
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. |
|
|
|
Dealing with something that's missing |
|
|
255 | (1) |
|
Why you might need an optional |
|
|
256 | (3) |
|
Optionals and handling missing data |
|
|
259 | (1) |
|
|
260 | (3) |
|
Unwrapping optionals with guard |
|
|
263 | (1) |
|
|
264 | (10) |
|
|
274 | (2) |
|
Here's our new Queue type |
|
|
276 | (6) |
10 Getting started with SwiftUI: User Interfaces...Swiftly |
|
|
It's time to use your toolbox full of Swift techniques, features, and components: you're going to start building user interfaces. |
|
|
|
|
|
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. |
|
|
|
What's a UI framework, anyway? |
|
|
282 | (2) |
|
|
284 | (4) |
|
|
288 | (1) |
|
Making a list, checking it...quite a few times, to get it perfect |
|
|
289 | (2) |
|
User interfaces with state |
|
|
291 | (1) |
|
|
292 | (3) |
|
Let's see how far you've come |
|
|
295 | (2) |
|
Customizing your views with modifiers |
|
|
297 | (3) |
|
Create a new SwiftUI Xcode project, for iOS |
|
|
300 | (2) |
|
Your Xcode will look something like this |
|
|
302 | (1) |
|
Create a new type to store a todo item in |
|
|
303 | (3) |
|
Make sure each todo item can be uniquely identified |
|
|
306 | (1) |
|
Create a user interface for the app |
|
|
307 | (7) |
|
Implement a way to save the list of todos |
|
|
314 | (5) |
|
So, that's a UI framework? |
|
|
319 | (9) |
11 Putting SwiftUI into Practice: Circles, Timers, Buttons-Oh, My! |
|
|
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! |
|
|
|
What fancy things can be done with a UI framework? |
|
|
328 | (3) |
|
Create a new SwiftUI Xcode project, for iOS |
|
|
331 | (2) |
|
The Executive Timer UI and features |
|
|
333 | (1) |
|
Creating the basic elements of the app |
|
|
334 | (1) |
|
|
335 | (1) |
|
Setting up the UI of the Executive Timer |
|
|
336 | (1) |
|
Coding the pieces of the UI |
|
|
337 | (3) |
|
Combining the three elements |
|
|
340 | (2) |
|
|
342 | (2) |
|
Tabbed views for a clean UI |
|
|
344 | (2) |
|
Build a TabView containing your views |
|
|
346 | (4) |
|
Creating a new tabbed ContentView |
|
|
350 | (1) |
|
Creating the tabs and the Tab View |
|
|
351 | (2) |
|
Running your new tabbed Executive Timer |
|
|
353 | (5) |
12 Apps, Web, and beyond: Putting It All Together |
|
|
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. |
|
|
|
|
358 | (5) |
|
A recipe for a welcome screen |
|
|
363 | (1) |
|
Step by step assembly of the welcome screen |
|
|
364 | (6) |
|
|
370 | (1) |
|
It's time for our old friend |
|
|
371 | (2) |
|
Building an app with multiple views that share state |
|
|
373 | (1) |
|
Building a two-view score tracker |
|
|
374 | (1) |
|
|
374 | (1) |
|
|
375 | (1) |
|
|
376 | (1) |
|
|
377 | (6) |
|
|
383 | (4) |
|
Meet Vapor, the Swift web framework |
|
|
387 | (3) |
|
Sending data over the web with Vapor |
|
|
390 | |