Introduction |
|
xv | |
Learning Swift |
|
xv | |
Why Swift? |
|
xv | |
What About Objective-C? |
|
xvi | |
Prerequisites |
|
xvi | |
How This Book Is Organized |
|
xvi | |
How to Use This Book |
|
xvii | |
Challenges and For the More Curious |
|
xviii | |
Typographical Conventions |
|
xviii | |
Necessary Hardware and Software |
|
xviii | |
Before You Begin |
|
xviii | |
|
|
1 | (18) |
|
|
3 | (10) |
|
Getting Started with Xcode |
|
|
4 | (2) |
|
|
6 | (1) |
|
|
7 | (1) |
|
Troubleshooting Playgrounds |
|
|
8 | (1) |
|
Varying Variables and Printing to the Console |
|
|
8 | (3) |
|
|
11 | (1) |
|
|
11 | (1) |
|
|
12 | (1) |
|
2 Types, Constants, and Variables |
|
|
13 | (6) |
|
|
13 | (2) |
|
|
15 | (1) |
|
|
16 | (1) |
|
|
17 | (2) |
|
|
19 | (60) |
|
|
21 | (6) |
|
|
21 | (3) |
|
|
24 | (1) |
|
|
25 | (1) |
|
|
26 | (1) |
|
|
26 | (1) |
|
|
27 | (14) |
|
|
27 | (2) |
|
Creating Integer Instances |
|
|
29 | (2) |
|
|
31 | (4) |
|
|
32 | (1) |
|
|
33 | (1) |
|
|
33 | (2) |
|
Converting Between Integer Types |
|
|
35 | (1) |
|
|
36 | (2) |
|
|
38 | (1) |
|
|
39 | (1) |
|
For the More Curious: Numeric Literals |
|
|
39 | (2) |
|
|
41 | (12) |
|
|
42 | (6) |
|
|
44 | (1) |
|
|
45 | (2) |
|
|
47 | (1) |
|
Tuples and Pattern Matching |
|
|
48 | (2) |
|
|
50 | (2) |
|
|
52 | (1) |
|
|
52 | (1) |
|
|
53 | (12) |
|
|
54 | (5) |
|
|
57 | (2) |
|
|
59 | (1) |
|
|
60 | (1) |
|
Control Transfer Statements in Loops |
|
|
61 | (3) |
|
|
64 | (1) |
|
|
65 | (14) |
|
|
65 | (3) |
|
|
67 | (1) |
|
|
68 | (6) |
|
|
68 | (2) |
|
|
70 | (4) |
|
|
74 | (1) |
|
|
74 | (1) |
|
For the More Curious: Substrings |
|
|
74 | (3) |
|
For the More Curious: Multiline Strings |
|
|
77 | (2) |
|
III Collections and Functions |
|
|
79 | (78) |
|
|
81 | (12) |
|
|
82 | (2) |
|
Accessing and Modifying Arrays |
|
|
84 | (3) |
|
|
87 | (1) |
|
|
88 | (1) |
|
|
89 | (1) |
|
|
89 | (3) |
|
|
92 | (1) |
|
|
92 | (1) |
|
|
93 | (12) |
|
|
94 | (2) |
|
|
96 | (3) |
|
Implicitly Unwrapped Optionals |
|
|
99 | (1) |
|
|
100 | (1) |
|
Modifying an Optional in Place |
|
|
101 | (1) |
|
The Nil Coalescing Operator |
|
|
102 | (1) |
|
|
103 | (1) |
|
|
103 | (1) |
|
|
103 | (2) |
|
|
105 | (8) |
|
|
106 | (1) |
|
Accessing and Modifying Values |
|
|
107 | (2) |
|
Adding and Removing Values |
|
|
109 | (1) |
|
Looping over a Dictionary |
|
|
110 | (1) |
|
|
111 | (1) |
|
Translating a Dictionary to an Array |
|
|
111 | (1) |
|
|
112 | (1) |
|
|
112 | (1) |
|
|
113 | (10) |
|
|
113 | (1) |
|
|
114 | (1) |
|
|
115 | (4) |
|
|
116 | (1) |
|
|
117 | (1) |
|
|
118 | (1) |
|
|
119 | (2) |
|
|
121 | (1) |
|
|
121 | (2) |
|
|
123 | (16) |
|
|
124 | (1) |
|
|
125 | (5) |
|
|
126 | (1) |
|
|
127 | (2) |
|
|
129 | (1) |
|
Returning from a Function |
|
|
130 | (1) |
|
Nested Function Definitions and Scope |
|
|
131 | (1) |
|
|
132 | (1) |
|
|
133 | (1) |
|
Exiting Early from a Function |
|
|
134 | (1) |
|
|
135 | (1) |
|
|
136 | (1) |
|
|
136 | (1) |
|
For the More Curious: Void |
|
|
137 | (1) |
|
For the More Curious: Variadic Parameters |
|
|
138 | (1) |
|
|
139 | (18) |
|
|
139 | (2) |
|
Closure Expression Syntax |
|
|
141 | (3) |
|
|
144 | (2) |
|
Closures Capture Their Enclosing Scope |
|
|
146 | (4) |
|
|
150 | (1) |
|
|
150 | (4) |
|
|
151 | (1) |
|
|
152 | (1) |
|
|
153 | (1) |
|
|
154 | (1) |
|
|
154 | (1) |
|
|
154 | (1) |
|
For the More Curious: Functions as Return Types |
|
|
155 | (2) |
|
IV Enumerations, Structures, and Classes |
|
|
157 | (104) |
|
|
159 | (16) |
|
|
159 | (4) |
|
Enumerations with Raw Values |
|
|
163 | (1) |
|
|
165 | (4) |
|
|
169 | (3) |
|
|
172 | (1) |
|
|
172 | (1) |
|
For the More Curious: Recursive Enumerations |
|
|
172 | (3) |
|
|
175 | (24) |
|
|
175 | (6) |
|
|
181 | (3) |
|
|
184 | (2) |
|
|
185 | (1) |
|
|
186 | (8) |
|
|
186 | (1) |
|
|
187 | (7) |
|
Looking Ahead: What Is the Real Difference? |
|
|
194 | (3) |
|
|
197 | (1) |
|
|
197 | (1) |
|
For the More Curious: Type Methods |
|
|
197 | (2) |
|
|
199 | (18) |
|
|
200 | (1) |
|
|
201 | (1) |
|
|
201 | (3) |
|
|
204 | (2) |
|
|
205 | (1) |
|
|
206 | (2) |
|
|
208 | (3) |
|
|
211 | (4) |
|
Controlling getter and setter visibility |
|
|
213 | (2) |
|
|
215 | (1) |
|
|
215 | (1) |
|
|
215 | (1) |
|
For the More Curious: Key Paths |
|
|
216 | (1) |
|
|
217 | (22) |
|
|
217 | (1) |
|
|
218 | (5) |
|
Default initializers for structs |
|
|
218 | (1) |
|
Custom initializers for structs |
|
|
219 | (4) |
|
|
223 | (9) |
|
Default initializers for classes |
|
|
223 | (1) |
|
Initialization and class inheritance |
|
|
224 | (6) |
|
Required initializers for classes |
|
|
230 | (1) |
|
|
231 | (1) |
|
|
232 | (3) |
|
A failable Town initializer |
|
|
233 | (2) |
|
Initialization Going Forward |
|
|
235 | (1) |
|
|
236 | (1) |
|
|
236 | (1) |
|
For the More Curious: Initializer Parameters |
|
|
237 | (2) |
|
18 Value vs Reference Types |
|
|
239 | (22) |
|
|
239 | (3) |
|
|
242 | (2) |
|
Constant Value and Reference Types |
|
|
244 | (2) |
|
Using Value and Reference Types Together |
|
|
246 | (1) |
|
|
247 | (2) |
|
|
249 | (2) |
|
|
251 | (1) |
|
For the More Curious: Copy on Write |
|
|
252 | (9) |
|
|
261 | (134) |
|
|
263 | (18) |
|
Formatting a Table of Data |
|
|
264 | (4) |
|
|
268 | (3) |
|
|
271 | (2) |
|
|
273 | (1) |
|
|
274 | (2) |
|
|
276 | (2) |
|
|
278 | (2) |
|
|
280 | (1) |
|
|
280 | (1) |
|
|
280 | (1) |
|
|
280 | (1) |
|
|
281 | (8) |
|
Extending an Existing Type |
|
|
282 | (1) |
|
|
283 | (5) |
|
Using extensions to add protocol conformance |
|
|
284 | (1) |
|
Adding an initializer with an extension |
|
|
285 | (1) |
|
Nested types and extensions |
|
|
286 | (1) |
|
|
287 | (1) |
|
|
288 | (1) |
|
|
288 | (1) |
|
|
289 | (22) |
|
|
290 | (2) |
|
Generic Functions and Methods |
|
|
292 | (3) |
|
|
295 | (2) |
|
|
297 | (3) |
|
Type Constraints in where Clauses |
|
|
300 | (3) |
|
Generic Composition and Opaque Types |
|
|
303 | (5) |
|
|
308 | (1) |
|
|
308 | (1) |
|
|
308 | (1) |
|
For the More Curious: Understanding Optionals |
|
|
309 | (2) |
|
|
311 | (14) |
|
|
312 | (1) |
|
|
313 | (2) |
|
Self Types and Type Values |
|
|
315 | (1) |
|
Protocol Extension where Clauses |
|
|
316 | (2) |
|
Default Implementations with Protocol Extensions |
|
|
318 | (2) |
|
|
320 | (2) |
|
|
322 | (1) |
|
|
322 | (1) |
|
|
322 | (1) |
|
For the More Curious: Polymorphism and Protocol-Oriented Programming |
|
|
323 | (2) |
|
|
325 | (24) |
|
|
325 | (1) |
|
|
326 | (10) |
|
|
336 | (1) |
|
|
337 | (5) |
|
Handling Errors by Sticking Your Head in the Sand |
|
|
342 | (2) |
|
Swift Error-Handling Philosophy |
|
|
344 | (2) |
|
|
346 | (1) |
|
|
346 | (1) |
|
|
346 | (1) |
|
For the More Curious: Storing Failable Results for Later |
|
|
347 | (2) |
|
24 Memory Management and ARC |
|
|
349 | (20) |
|
|
349 | (1) |
|
|
350 | (8) |
|
Breaking Strong Reference Cycles with weak |
|
|
358 | (1) |
|
Reference Cycles with Closures |
|
|
359 | (5) |
|
Escaping and Non-Escaping Closures |
|
|
364 | (2) |
|
|
366 | (1) |
|
|
366 | (1) |
|
|
366 | (1) |
|
For the More Curious: A Bit of History |
|
|
367 | (1) |
|
For the More Curious: Do I Have the Only Reference? |
|
|
368 | (1) |
|
25 Equatable, Comparable, and Hashable |
|
|
369 | (16) |
|
|
369 | (4) |
|
|
372 | (1) |
|
Buy one method, get another free! |
|
|
372 | (1) |
|
|
373 | (3) |
|
|
375 | (1) |
|
|
376 | (3) |
|
|
377 | (2) |
|
|
379 | (1) |
|
|
379 | (1) |
|
|
380 | (1) |
|
|
380 | (1) |
|
For the More Curious: Custom Operators |
|
|
381 | (4) |
|
|
385 | (10) |
|
Defining a Property Wrapper |
|
|
386 | (5) |
|
|
389 | (2) |
|
Accessing the Wrapper Itself |
|
|
391 | (1) |
|
Projecting Related Values |
|
|
392 | (2) |
|
|
394 | (1) |
|
|
394 | (1) |
|
|
394 | (1) |
|
|
395 | (66) |
|
27 Command-Line Utilities |
|
|
397 | (26) |
|
Introduction to the Command Line |
|
|
397 | (3) |
|
|
400 | (6) |
|
Loading the words from disk |
|
|
405 | (1) |
|
Retrieving Command-Line Arguments |
|
|
406 | (3) |
|
Receiving Input Interactively |
|
|
409 | (3) |
|
Running Your App from the Command Line |
|
|
412 | (1) |
|
Parsing Command-Line Arguments with ArgumentParser |
|
|
413 | (8) |
|
Adding ArgumentParser to your project |
|
|
413 | (4) |
|
Declaring arguments for ArgumentParser to parse |
|
|
417 | (4) |
|
|
421 | (1) |
|
|
421 | (2) |
|
|
423 | (36) |
|
Getting Started with TahDoodle |
|
|
424 | (3) |
|
|
427 | (4) |
|
|
431 | (7) |
|
|
438 | (9) |
|
Sharing references to value-type data |
|
|
441 | (5) |
|
Interlude: Troubleshooting with property observers |
|
|
446 | (1) |
|
Observing Changes to the Store |
|
|
447 | (2) |
|
Saving and Loading User Data |
|
|
449 | (5) |
|
|
454 | (3) |
|
|
457 | (1) |
|
|
457 | (1) |
|
|
457 | (2) |
|
|
459 | (2) |
|
|
459 | (1) |
|
|
459 | (1) |
|
An Invitation to the Community |
|
|
460 | (1) |
Index |
|
461 | |