ENGLISH

Swift In Depth

Book information

Publisher
Manning Publications
Year
2019
Language
english
Format
PDF
Filesize
5 MB (4810723 bytes)
Edition
1st Edition
Pages
439\439
Time added
2019-02-07 16:06:36

Description

Tjeerd in 't Veen......Page 2 Copyright......Page 3 Preface......Page 5 Acknowledgements......Page 7 WHY THIS BOOK?......Page 8 WHAT THIS BOOK IS NOT......Page 9 Chapter 2: Modeling data with enums......Page 10 Chapter 6: Effortless error handling......Page 11 Chapter 8: Putting the pro in protocol-oriented programming......Page 12 Chapter 11: Asynchronous error handling with Result......Page 13 Chapter 12: Protocol extensions......Page 14 Chapter 15: Where to Swift from here......Page 15 BOOK FORUM......Page 16 ABOUT THE COVER ILLUSTRATION......Page 17 1.1. THE SWEET SPOT OF SWIFT......Page 18 1.2. BELOW THE SURFACE......Page 19 1.3.2. Strictness......Page 20 Listing 1.1. Trying to equate to types......Page 21 Listing 1.2. Protocols can surprise us......Page 22 Listing 1.3. A pyramid of doom......Page 23 1.3.6. Compile times......Page 24 1.5. HOW TO MAKE THE MOST OF THIS BOOK......Page 25 SUMMARY......Page 26 Chapter 2. Modeling data with enums......Page 27 2.1.1. Modeling data with a struct......Page 28 Figure 2.1. A chat application......Page 29 Listing 2.2. A text message......Page 30 Listing 2.4. An invalid message with conflicting properties......Page 31 Listing 2.6. Message as an enum (with values)......Page 32 Listing 2.8. Logging messages......Page 33 2.2. ENUMS FOR POLYMORPHISM......Page 34 What are these brackets?......Page 35 Listing 2.15. Matching on the dateType enum......Page 36 2.3. ENUMS INSTEAD OF SUBCLASSING......Page 37 Listing 2.19. The Cycle struct......Page 38 Figure 2.3. A subclassing hierarchy......Page 39 Figure 2.4. A refactored subclassing hierarchy......Page 40 Listing 2.23. Pattern matching on a workout......Page 41 2.3.6. Exercises......Page 42 Listing 2.25. The Day enum......Page 43 Listing 2.28. BooleanContainer has four possible variations......Page 44 Listing 2.30. A PaymentStatus struct......Page 45 2.4.4. Exercise......Page 46 Listing 2.33. Enum with raw values, with string values omitted......Page 48 Listing 2.36. Unexpected parameters......Page 49 2.5.2. Matching on strings......Page 50 Listing 2.39. Unknown icon......Page 51 Listing 2.42. Adding a custom initializer to ImageType......Page 52 Listing 2.43. Passing different strings......Page 53 2.6. CLOSING THOUGHTS......Page 54 ANSWERS......Page 55 Listing 3.2. Value changes over time with computed properties......Page 58 Listing 3.3. The lifecycle of a running exercise......Page 59 Listing 3.4. The Run struct......Page 60 Listing 3.6. Run with computed properties......Page 61 3.2. LAZY PROPERTIES......Page 62 Listing 3.8. Creating a LearningPlan......Page 63 Pattern matching......Page 64 Listing 3.12. LearningPlan with a lazy property......Page 65 Listing 3.13. Adding a custom initializer......Page 66 Listing 3.15. Overriding the contents of LearningPlan......Page 67 Listing 3.18. LearningPlan level now mutable......Page 68 Figure 3.1. Copying after initializing a lazy description......Page 69 Listing 3.20. Copying before lazy loading......Page 70 3.2.6. Exercises......Page 71 Listing 3.21. Trimming whitespace......Page 73 3.3.2. Trigger property observers from initializers......Page 74 Listing 3.24. Adding a defer closure to the initializer......Page 75 3.3.3. Exercises......Page 76 ANSWERS......Page 77 Chapter 4. Making optionals second nature......Page 81 4.2. CLEAN OPTIONAL UNWRAPPING......Page 82 Listing 4.2. Optionals are an enum......Page 83 Listing 4.4. Unwrapping via if let......Page 84 Listing 4.5. Matching on optional with syntactic sugar......Page 85 Listing 4.8. Combining pattern matching with optional unwrapping......Page 86 Listing 4.11. When an optional is nil......Page 87 Listing 4.12. Conforming to the CustomStringConvertible protocol......Page 88 Listing 4.14. An order confirmation message......Page 89 Listing 4.15. The Customer struct......Page 90 4.5. RETURNING OPTIONAL STRINGS......Page 91 Listing 4.18. Unwrapping the optional displayName......Page 92 4.6. GRANULAR CONTROL OVER OPTIONALS......Page 93 Listing 4.20. Defaulting back on a value with the nil-coalescing operator......Page 94 Listing 4.23. Unwrapping an optional before pattern matching......Page 95 4.8.1. Exercise......Page 96 4.9. CHAINING OPTIONALS......Page 97 Listing 4.28. Unwrapping a chained optional......Page 98 Listing 4.30. Receiving an optional Boolean......Page 99 Listing 4.33. Converting a Boolean to an enum......Page 100 Listing 4.34. The UserPreference enum......Page 101 4.10.5. Exercise......Page 102 4.11. FORCE UNWRAPPING GUIDELINES......Page 103 Postponing error handling......Page 104 Listing 4.39. Crashing manually......Page 105 Listing 4.40. Introducing an IUO......Page 106 Figure 4.2. Starting the process monitor......Page 107 Listing 4.41. Introducing ChatService and ProcessMonitor......Page 108 4.12.3. Exercise......Page 109 ANSWERS......Page 110 5.1. STRUCT INITIALIZER RULES......Page 113 Listing 5.3. Omitting a property......Page 114 Listing 5.4. Creating your initializer......Page 115 Listing 5.6. Restoring the Player struct......Page 116 5.1.3. Exercises......Page 117 5.2.1. Creating a board game superclass......Page 118 5.2.2. The initializers of BoardGame......Page 119 Listing 5.8. The BoardGame superclass......Page 120 Listing 5.9. Initializing BoardGame......Page 121 Listing 5.10. MutabilityLand inherits all the BoardGame initializers......Page 122 5.2.4. Losing convenience initializers......Page 123 Listing 5.12. Creating a designated initializer for MutabilityLand......Page 124 5.2.5. Getting the superclass initializers back......Page 125 Listing 5.15. MutabilityLand overrides the designated initializer......Page 126 5.2.6. Exercise......Page 127 Figure 5.5. MutabilityLand performs a convenience override on a designated initializer.......Page 129 Listing 5.17. A convenience override......Page 130 Listing 5.18. MutabilityLandJunior......Page 131 5.3.3. Exercise......Page 132 Listing 5.20. Factory methods in action......Page 133 Listing 5.23. Adding the required keyword to initializers......Page 134 Protocols......Page 135 Listing 5.27. BoardGame is now a final class......Page 136 5.5. CLOSING THOUGHTS......Page 137 ANSWERS......Page 138 Chapter 6. Effortless error handling......Page 142 Listing 6.1. An enum Error......Page 143 Listing 6.3. Parsing location strings......Page 144 6.1.3. Swift doesn’t reveal errors......Page 145 Listing 6.4. Adding error information to a function......Page 146 Listing 6.5. The TodoList that mutates state on errors......Page 147 Listing 6.6. TodoList works with temporary values......Page 148 Recovery code with defer......Page 149 Listing 6.7. Recovering writing to files with defer......Page 150 6.2.1. Propagating errors......Page 151 Listing 6.8. The RecipeExtractor......Page 152 6.2.2. Adding technical details for troubleshooting......Page 154 Listing 6.10. Matching on a specific error......Page 155 Listing 6.11. Implementing LocalizedError......Page 156 Listing 6.12. Implementing NSError......Page 158 Listing 6.13. An ErrorHandler with function overloads......Page 159 Implementing the centralized error handler......Page 160 Listing 6.14. RecipeExtractor becomes throwing......Page 161 6.2.4. Exercises......Page 162 Listing 6.15. Validating a phone number......Page 163 Listing 6.16. The PhoneNumber type......Page 164 6.3.2. try?......Page 165 Listing 6.19. Returning an optional......Page 166 SUMMARY......Page 167 ANSWERS......Page 168 Chapter 7. Generics......Page 170 Listing 7.2. firstLast with an array of strings......Page 171 A cup of T?......Page 172 Listing 7.6. Custom types......Page 173 Listing 7.7. Wrapping a value inside an array......Page 174 Listing 7.9. The wrapValue function......Page 175 7.1.3. Exercise......Page 176 Listing 7.11. The lowest function (will compile soon, but not yet)......Page 177 7.2.2. The Equatable and Comparable protocols......Page 178 Listing 7.13. Comparable......Page 179 7.2.4. Implementing Comparable......Page 180 Listing 7.18. Comparable in action......Page 181 7.3.1. The Hashable protocol......Page 182 Listing 7.20. The Hashable protocol......Page 183 Listing 7.21. Combining constraints......Page 184 Listing 7.23. The generic type Wrapped......Page 185 Listing 7.27. Introducing Pair......Page 186 Pop quiz......Page 187 7.4.4. Conforming to Hashable......Page 188 Being explicit......Page 189 Listing 7.34. Implementing Hashable manually......Page 190 7.5.1. Subtyping and invariance......Page 191 7.5.2. Invariance in Swift......Page 192 Listing 7.38. Cache......Page 193 Listing 7.40. Swift’s types are covariant......Page 194 ANSWERS......Page 195 8.1. RUNTIME VERSUS COMPILE TIME......Page 199 Listing 8.1. A CryptoCurrency protocol......Page 200 Listing 8.3. Introducing a portfolio (won’t fully work yet!)......Page 201 Listing 8.4. Trying to add different coins to the Portfolio......Page 202 Listing 8.7. A dynamic Portfolio......Page 203 8.1.5. Choosing between compile time and runtime......Page 204 Listing 8.10. A generic protocol vs. a runtime protocol......Page 205 8.1.7. Exercises......Page 206 Listing 8.11. The Worker protocol......Page 208 Figure 8.1. Trying to conform to Worker......Page 209 Listing 8.12. Worker without associated types......Page 210 Listing 8.13. A Worker protocol (won’t compile yet!)......Page 211 Listing 8.15. Worker with associated types......Page 212 Listing 8.16. Mailjob (implementation omitted)......Page 213 Listing 8.17. The FileRemover......Page 214 Self requirements......Page 215 8.2.8. Exercise......Page 216 8.3. PASSING PROTOCOLS WITH ASSOCIATED TYPES......Page 217 Listing 8.19. Passing multiple workers......Page 218 Listing 8.20. Constraining the Input associated type......Page 219 Listing 8.21. Calling the ImageProcessor......Page 220 Listing 8.23. The ImageProcessor type......Page 221 Listing 8.25. No need to constrain anymore......Page 223 8.3.4. Exercises......Page 224 SUMMARY......Page 225 ANSWERS......Page 226 Note......Page 229 Chapter 9. Iterators, sequences, and collections......Page 230 Listing 9.2. Using makeIterator()......Page 231 Listing 9.3. IteratorProtocol in Swift......Page 232 9.1.3. The Sequence protocol......Page 233 Listing 9.5. Sequence protocol (not complete)......Page 234 9.2.1. filter......Page 235 Listing 9.7. Using forEach......Page 236 9.2.4. Lazy iteration......Page 237 Listing 9.10. Using lazy......Page 238 Listing 9.11. Preparing for a reduce......Page 239 Listing 9.12. reduce with less performance......Page 240 Note......Page 241 9.2.8. Exercises......Page 242 Figure 9.4. How Bag stores data......Page 243 Listing 9.15. Using Bag......Page 244 Listing 9.16. Looking inside Bag......Page 245 9.3.2. Creating a BagIterator......Page 246 Listing 9.18. Creating a BagIterator......Page 247 9.3.3. Implementing AnyIterator......Page 248 9.3.4. Implementing ExpressibleByArrayLiteral......Page 249 Listing 9.22. Implementing ExpressibleByArrayLiteral......Page 250 9.4. THE COLLECTION PROTOCOL......Page 251 Listing 9.24. Indexing a String......Page 252 9.4.2. MutableCollection......Page 253 String doesn’t conform to MutableCollection......Page 254 Listing 9.28. removeAll in action......Page 255 Listing 9.30. Using reversed() instead......Page 256 Listing 9.32. Using zip......Page 257 Listing 9.34. Day......Page 258 Listing 9.35. TravelPlan......Page 259 Listing 9.36. Implementing Collection......Page 260 Listing 9.39. Implementing subscripts......Page 261 Listing 9.40. Implementing ExpressibleByArrayLiteral......Page 262 9.5.5. Exercise......Page 263 ANSWERS......Page 264 Chapter 10. Understanding map, flatMap, and compactMap......Page 268 Listing 10.1. Transforming an array......Page 269 Listing 10.2. Refactoring a for loop with map......Page 271 Listing 10.4. Transforming data with a pipeline......Page 272 Listing 10.5. Turning tuples into a dictionary......Page 273 Listing 10.7. Mapping over a dictionary’s values......Page 274 10.1.3. Exercises......Page 275 Listing 10.8. Mapping over a Range sequence......Page 276 Stripping emojis......Page 277 Listing 10.10. Detect if Unicode is in emoji range......Page 278 Listing 10.11. The Cover class......Page 279 Listing 10.12. Mapping over a title......Page 280 Note......Page 281 Listing 10.14. Chaining map operations......Page 282 10.3.4. Exercise......Page 283 Figure 10.4. Removing emojis on multiple types......Page 284 10.5.2. When map doesn’t cut it......Page 285 Listing 10.16. Removing double nesting with a force unwrap......Page 286 Listing 10.17. Using flatMap to remove double-nested optional......Page 287 Listing 10.18. Safe halving function......Page 288 Listing 10.19. A pyramid of doom......Page 289 10.5.4. flatMapping over an optional......Page 290 Listing 10.22. Multiple halving operations on flatMap......Page 291 Shortcircuiting with flatMap......Page 292 Listing 10.23. Short-circuiting......Page 293 Listing 10.24. A shorter notation......Page 294 Figure 10.7. Flattening a collection with flatMap......Page 295 Listing 10.28. interspersed......Page 296 Listing 10.30. Generating a deck of cards......Page 297 Listing 10.31. Creating optional URLs......Page 298 Listing 10.34. Using a for loop to filter an optional array......Page 299 10.6.4. Nesting or chaining......Page 300 10.6.5. Exercises......Page 301 SUMMARY......Page 302 ANSWERS......Page 303 Chapter 11. Asynchronous error handling with Result......Page 308 11.1.1. Getting your hands on Result......Page 309 Listing 11.1. The Result type......Page 311 Listing 11.2. Performing a network call......Page 312 11.1.4. Creating an API using Result......Page 313 Listing 11.3. A response with Result......Page 314 Listing 11.5. The URLSession’s response......Page 315 Listing 11.6. Converting a response and error into a Result......Page 316 11.2.1. Typealiasing for convenience......Page 317 Listing 11.10. The search function implementation......Page 318 11.3. TRANSFORMING VALUES INSIDE RESULT......Page 320 Figure 11.2. Mapping over an error inside Result......Page 321 Listing 11.11. Mapping over an error and value......Page 322 11.3.2. flatMapping over Result......Page 323 Listing 11.12. flatMaping over Result......Page 324 11.3.3. Exercises......Page 325 Listing 11.13. The parseData function......Page 326 Listing 11.15. Passing a throwing function to Result......Page 327 Listing 11.16. Creating a Result from parseData......Page 328 11.4.3. Weaving errors through a pipeline......Page 329 Listing 11.17. A longer pipeline......Page 330 Short-circuiting a chaining operation......Page 331 11.4.5. Exercise......Page 332 Listing 11.18. Creating a Result with AnyError......Page 333 Listing 11.19. Returning different errors......Page 334 Matching with AnyError......Page 335 Listing 11.21. The Service protocol......Page 336 Listing 11.22. Implementing the Service protocol......Page 337 Listing 11.24. From the Swift source......Page 338 11.7. CLOSING THOUGHTS......Page 339 ANSWERS......Page 340 Chapter 12. Protocol extensions......Page 347 Join me!......Page 348 Listing 12.1. A protocol extension......Page 349 Listing 12.2. Implementing a protocol with default implementation......Page 350 Listing 12.3. ResponseHandler......Page 351 Listing 12.4. The Email and Mailer types......Page 352 Figure 12.3. ValidatingMailer inheriting from Mailer......Page 353 Listing 12.6. SMTPClient......Page 354 Figure 12.4. SMTPClient implementing Mailer and MailValidator......Page 355 Listing 12.8. Intersecting Mailer and MailValidator......Page 356 Listing 12.10. Implementing two protocols to get a free method......Page 357 12.2.5. Exercise......Page 358 12.3.2. Overriding with protocol inheritance......Page 359 Listing 12.12. Overrides in action......Page 360 12.3.3. Exercise......Page 361 12.4.1. Opting in to extensions......Page 362 Listing 12.14. Opting in for benefits......Page 363 Listing 12.16. Extending Array......Page 364 Listing 12.18. Testing out the unique() method......Page 365 Figure 12.8. Specializing an associated type......Page 366 Listing 12.20. unique on Set......Page 367 Listing 12.22. Extending Collection......Page 368 12.7. EXTENDING SEQUENCE......Page 369 Listing 12.26. Looking at filter......Page 370 ContiguousArray......Page 371 Listing 12.28. Get the first lines......Page 372 Listing 12.29. Extending Sequence with take(while:)......Page 373 Listing 12.30. The inspect method in action......Page 374 12.7.4. Exercise......Page 375 ANSWERS......Page 376 13.1. DEPENDENCY INJECTION......Page 379 Figure 13.1. Mocking......Page 380 Listing 13.3. The WeatherAPI class......Page 381 13.1.3. Constraining an associated type......Page 382 Listing 13.5. Implementing DataTask......Page 383 Listing 13.6. An offline task......Page 384 Listing 13.7. Swapping out implementations......Page 385 Listing 13.8. Creating a MockTask and MockSession......Page 386 Listing 13.9. Testing the API......Page 387 Listing 13.10. Extending Session with a Result type......Page 388 13.1.7. Exercise......Page 389 Listing 13.12. Auto Equatable......Page 390 Listing 13.13. The Track protocol......Page 391 Listing 13.15. Can’t use Array as a Track type......Page 392 Warning......Page 393 Listing 13.18. Extending Optional......Page 394 Figure 13.3. A type mimicking an inner type......Page 395 Listing 13.22. Inside CachedValue......Page 396 Making your type conditionally conformant......Page 397 Listing 13.23. Conditional conformance on CachedValue......Page 398 Listing 13.24. CachedValue is now Equatable, Comparable, and Hashable......Page 399 Listing 13.25. PokerGame......Page 400 Listing 13.28. A resolved generic......Page 401 Listing 13.29. PokerGame......Page 402 Note......Page 403 Listing 13.30. AnyPokerGame in action......Page 404 Creating AnyPokerGame......Page 405 Listing 13.32. Implementing Hashable......Page 406 Note......Page 407 13.3.3. Exercise......Page 408 13.4. AN ALTERNATIVE TO PROTOCOLS......Page 409 Listing 13.34. Implementing the Validator protocol......Page 410 Listing 13.35. Introducing Validator......Page 411 Listing 13.36. Combining validators......Page 412 SUMMARY......Page 413 ANSWERS......Page 414 Chapter 14. Delivering quality Swift code......Page 417 Figure 14.1. Quick Help pop-up......Page 418 Listing 14.1. The turn with Quick Help notations......Page 419 Listing 14.2. Adding callouts......Page 420 14.1.3. Documentation as HTML with Jazzy......Page 421 Figure 14.4. Jazzy documentation......Page 422 Listing 14.4. Message with a “why” comment......Page 423 14.2.4. Comments are no bandage for bad names......Page 424 14.3. SETTLING ON A STYLE......Page 425 Figure 14.5. SwiftLint in action......Page 426 Figure 14.6. Xcode build phases......Page 427 Listing 14.5. SwiftLint configuration file......Page 428 14.3.6. Autocorrecting SwiftLint rules......Page 430 14.4. KILL THE MANAGERS......Page 431 14.4.2. Attacking managers......Page 432 14.4.3. Paving the road for generics......Page 433 Figure 14.10. Reusing generic types......Page 434 Listing 14.6. Getting important places......Page 435 14.5.3. Generic naming......Page 436 SUMMARY......Page 437 15.2. EXPLORE THE SWIFT PACKAGE MANAGER......Page 438 15.4.2. Final words......Page 439

Similar books