Learn How to Program with Swift 5.5!Swift is the easiest way to get started developing on Apple's platforms: iOS, iPadOS, macOS, watchOS and tvOS. In this book, you'll learn the basics of Swift from getting started with playgrounds to simple operations to building your own types. Everything you'll learn is platform-neutral; you'll have a firm understanding of Swift by the end of this book, and you'll be ready to move on to whichever app platform you're interested in.Who This Book Is For:This book is for complete beginners to Swift. No prior programming experience is necessary!Topics Covered in The Swift ApprenticePlayground basics: Learn about the coding environment where you can quickly and easily try out your code as you learn.Basic types: Numbers and strings are the basic kinds of data in any app - learn how to use them in Swift.Flow control: Your code doesn't always run straight through - learn how to use conditions and decide what to do.Functions: Group your code together into reusable chunks to run and pass around.Collection types: Discover the many ways Swift offers to store and organize data into collections.Protocols & protocol-oriented programming: Define protocols to make your code more interface-based and compositional.Advanced topics: Learn how to create custom operators, organize your code, write tests, manage memory, serialize your types, concurrency and so much more.After reading this book and completing your Swift apprenticeship by working through the included exercises and challenges, you'll be ready to take on app development on the platform of your choice! Book License 14 What You Need 16 Book Source Code & Forums 17 About the Authors 19 About the Editors 20 Introduction 21 Who this book is for 22 How to use this book 22 What’s in store 23 Acknowledgments 24 Chapter 1: Expressions, Variables & Constants 26 How a computer works 27 Playgrounds 33 Getting started with Swift 38 Printing out 39 Arithmetic operations 40 Math functions 45 Naming data 46 Increment and decrement 50 Challenges 52 Key points 55 Chapter 2: Types & Operations 56 Type conversion 57 Strings 61 Strings in Swift 64 Tuples 67 A whole lot of number types 70 Type aliases 71 A peek behind the curtains: Protocols 72 Challenges 73 Key points 75 Chapter 3: Basic Control Flow 76 Comparison operators 77 The if statement 81 Loops 87 Challenges 90 Key points 93 Chapter 4: Advanced Control Flow 94 Countable ranges 95 For loops 96 Switch statements 102 Challenges 108 Key points 110 Chapter 5: Functions 111 Function basics 112 Functions as variables 120 Commenting your functions 124 Challenges 125 Key points 128 Chapter 6: Optionals 129 Introducing nil 130 Introducing optionals 131 Unwrapping optionals 133 Introducing guard 137 Nil coalescing 139 Challenges 140 Key points 143 Chapter 7: Arrays, Dictionaries & Sets 145 Mutable versus immutable collections 146 Arrays 146 What is an array? 147 When are arrays useful? 147 Creating arrays 147 Accessing elements 148 Modifying arrays 152 Iterating through an array 156 Running time for array operations 157 Dictionaries 158 Creating dictionaries 159 Accessing values 160 Modifying dictionaries 161 Sets 164 Challenges 166 Key points 170 Chapter 8: Collection Iteration With Closures 171 Closure basics 172 Custom sorting with closures 178 Iterating over collections with closures 178 Challenges 185 Key points 187 Chapter 9: Strings 188 Strings as collections 189 Strings as bi-directional collections 194 Raw strings 195 Substrings 197 Character properties 198 Encoding 200 Challenges 205 Key points 207 Chapter 10: Structures 209 Introducing structures 210 Accessing members 215 Introducing methods 216 Structures as values 218 Structures everywhere 219 Conforming to a protocol 219 Challenges 221 Key points 222 Chapter 11: Properties 223 Stored properties 224 Computed properties 226 Type properties 229 Property observers 230 Lazy properties 233 Challenges 236 Key points 237 Chapter 12: Methods 238 Method refresher 239 Introducing self 241 Introducing initializers 243 Introducing mutating methods 247 Type methods 248 Adding to an existing structure with extensions 249 Challenges 252 Key points 254 Chapter 13: Classes 255 Creating classes 256 Reference types 257 Understanding state and side effects 265 Extending a class using an extension 266 When to use a class versus a struct 267 Challenges 268 Key points 270 Chapter 14: Advanced Classes 271 Introducing inheritance 272 Inheritance and class initialization 280 When and why to subclass 285 Understanding the class lifecycle 288 Challenges 292 Key points 293 Chapter 15: Enumerations 294 Your first enumeration 295 Raw values 299 Associated values 302 Enumeration as a state machine 304 Iterating through all cases 305 Enumerations without any cases 306 Optionals 307 Challenges 308 Key points 310 Chapter 16: Protocols 311 Introducing protocols 312 Implementing protocols 317 Protocols in the Standard Library 323 Challenge 329 Key points 330 Chapter 17: Generics 331 Introducing generics 332 Anatomy of generic types 335 Arrays 340 Dictionaries 340 Optionals 341 Generic function parameters 343 Challenge 343 Key points 345 Chapter 18: Access Control, Code Organization & Testing 347 Introducing access control 349 Organizing code into extensions 358 Swift Package Manager 362 Testing 362 Challenges 367 Key points 369 Chapter 19: Custom Operators, Subscripts & Keypaths 370 Custom operators 371 Subscripts 376 Keypaths 382 Challenges 385 Key points 387 Chapter 20: Result Builders 388 Meet NSAttributedString 389 Creating a result builder 392 Key points 403 Chapter 21: Pattern Matching 404 Introducing patterns 405 Basic pattern matching 406 Patterns 408 Advanced patterns 412 Programming exercises 418 Expression pattern 420 Challenges 422 Key points 424 Chapter 22: Error Handling 425 What is error handling? 426 First level error handling with optionals 426 Error protocol 432 Throwing errors 433 Handling errors 434 Advanced error handling 437 Rethrows 440 Throwable properties 441 Throwable subscripts 443 Challenges 444 Key points 444 Chapter 23: Encoding & Decoding Types 445 Encodable and Decodable protocols 446 What is Codable? 446 Automatic encoding and decoding 447 Encoding and decoding custom types 448 Renaming properties with CodingKeys 449 Manual encoding and decoding 450 Writing tests for the Encoder and Decoder 453 Challenges 455 Challenge 5: Enumeration with associated values 456 Key points 457 Chapter 24: Memory Management 458 Reference cycles for classes 459 Reference cycles for closures 463 Challenges 467 Key points 470 Chapter 25: Value Types & Reference Types 471 Value types vs. reference types 472 Defining value semantics 475 Implementing value semantics 476 Recipes for value semantics 483 Challenges 485 Key points 487 Where to go from here? 488 Chapter 26: Property Wrappers 489 Basic example 490 Implementing CopyOnWrite 494 Wrappers, projections and other confusables 496 Projected values are handles 497 Challenges 500 Key points 502 Chapter 27: Protocol-Oriented Programming 503 Introducing protocol extensions 504 Default implementations 505 Understanding protocol extension dispatch 507 Type constraints 509 Protocol-oriented benefits 511 Why Swift is a protocol-oriented language 514 Challenges 516 Key points 518 Chapter 28: Advanced Protocols & Generics 519 Existential protocols 520 Non-existential protocols 520 Type erasure 526 Making a type erasure 528 Opaque return types 530 Challenges 532 Key points 534 Chapter 29: Concurrency 535 Basic tasks 536 Decoding an API — learning domains 541 Async/await in action 542 Asynchronous sequences 544 Ordering your concurrency 544 Asynchronous properties and subscripts 546 Introducing actors 547 Sendable 550 Challenges 552 Key points 554 Conclusion 555