Swift Apprentice
Total Page:16
File Type:pdf, Size:1020Kb
Swift Apprentice Swift Apprentice Ehab Amer, Alexis Gallagher, Matt Galloway, Eli Ganim, Ben Morrow and Cosmin Pupăză Copyright ©2019 Razeware LLC. Notice of Rights All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner. Notice of Liability This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software. Trademarks All trademarks and registered trademarks appearing in this book are the property of their own respective owners. raywenderlich.com 2 Swift Apprentice Table of Contents: Overview About the Cover...................................................................................... 14 What You Need........................................................................................ 18 Book License............................................................................................. 19 Book Source Code & Forums ............................................................. 20 Introduction.............................................................................................. 22 Section I: Swift Basics ....................................................... 26 Chapter 1: Expressions, Variables & Constants ............. 28 Chapter 2: Types & Operations............................................. 56 Chapter 3: Basic Control Flow .............................................. 75 Chapter 4: Advanced Control Flow..................................... 92 Chapter 5: Functions .............................................................. 108 Chapter 6: Optionals .............................................................. 126 Section II: Collection Types .......................................... 140 Chapter 7: Arrays, Dictionaries & Sets............................ 142 Chapter 8: Collection Iteration with Closures............. 168 Chapter 9: Strings.................................................................... 183 Section III: Building Your Own Types ........................ 202 Chapter 10: Structures.......................................................... 204 Chapter 11: Properties.......................................................... 216 Chapter 12: Methods ............................................................. 230 Chapter 13: Classes ................................................................ 247 raywenderlich.com 3 Swift Apprentice Chapter 14: Advanced Classes........................................... 263 Chapter 15: Enumerations................................................... 285 Chapter 16: Protocols............................................................ 302 Chapter 17: Generics ............................................................. 321 Section IV: Advanced Topics ........................................ 335 Chapter 18: Access Control, Code Organization and Testing.................................................................................. 337 Chapter 19: Custom Operators, Subscripts & Keypaths...................................................................................... 359 Chapter 20: Pattern Matching............................................ 376 Chapter 21: Error Handling................................................. 396 Chapter 22: Encoding & Decoding Types....................... 418 Chapter 23: Memory Management.................................. 430 Chapter 24: Value Types & Value Semantics................. 441 Chapter 25: Protocol-Oriented Programming............. 460 Chapter 26: Advanced Protocols & Generics............... 475 Conclusion .............................................................................................. 491 raywenderlich.com 4 Swift Apprentice Table of Contents: Extended About the Cover . 14 About the Authors . 16 About the Editors . 17 About the Artist. 17 What You Need . 18 Book License . 19 Book Source Code & Forums . 20 Introduction . 22 Who this book is for. 23 How to use this book. 23 What’s in store. 23 Acknowledgments . 24 Section I: Swift Basics . 26 Chapter 1: Expressions, Variables & Constants . 28 How a computer works . 29 Playgrounds. 35 Getting started with Swift . 39 Printing out . 40 Arithmetic operations . 41 Math functions. 46 Naming data . 47 Increment and decrement . 51 Challenges . 52 Key points. 54 Chapter 2: Types & Operations . 56 Type conversion. 57 raywenderlich.com 5 Swift Apprentice Strings . 61 Strings in Swift . 63 Tuples . 67 A whole lot of number types . 69 Type aliases . 70 A peek behind the curtains: Protocols . 71 Challenges . 72 Key points. 74 Chapter 3: Basic Control Flow . 75 Comparison operators. 76 The if statement . 80 Loops. 86 Challenges . 89 Key points. 91 Chapter 4: Advanced Control Flow. 92 Countable ranges . 93 For loops . 94 Switch statements . 99 Challenges. 105 Key points . 107 Chapter 5: Functions . 108 Function basics . 109 Functions as variables . 117 Commenting your functions. 120 Challenges. 122 Key points . 125 Chapter 6: Optionals . 126 Introducing nil . 127 Introducing optionals. 128 Unwrapping optionals . 130 raywenderlich.com 6 Swift Apprentice Introducing guard . 134 Nil coalescing. 136 Challenges. 137 Key points . ..