ebook img

Swift In Depth PDF

439 Pages·2019·4.59 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Swift In Depth

Swift in Depth Tjeerd in 't Veen Copyright For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: Project editor: Deirdre Hiam Copy editor: Darren Meiss Proofreaders: Carol Shields and Melody Dolab Technical proofreader: Edwin Chun Wing Kwok Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617295188 Printed in the United States of America 2 3 4 5 6 7 8 9 10 – SP – 23 22 21 20 19 Preface I started as an iOS developer in 2011. I loved to make iPhone apps and still do to this day. Besides doing mobile development work, I also was involved in some web development while learning Ruby. I loved the short powerful language, and wished I could use a compile-time language like Objective-C, but with the elegance and expressive nature of Ruby. Then Apple introduced Swift, and it seems like they listened. Swift was a fresh take on programming for me, combining the elegance of a dynamic language with the speed and safety of a static language. I never liked the Objective-C syntax. I used to say things like “Yeah, Objective-C is verbose, but it gets the job done.” But with Swift, however, I find reading and writing code very pleasing again, like I did with Ruby. I could finally use a static language and keep producing work while loving the language I’m working with. It was a good combination for me. However, it wasn’t love at first sight. Before I truly enjoyed Swift, I struggled a lot with it. Swift looks very friendly, but boy, was it tough sometimes. Everything needs to be safe at compile- time, and I could not mix and match types in arrays anymore. Meanwhile, Swift was only an early version and kept changing; it was hard to keep up. “What are generators? Oh, they are called iterators now? And why use guard? Can’t we use an if statement instead? Pfft, optionals are overrated; we can use simple nil checks?”, and so on. I wouldn’t even consider working with generics. However, I persevered and started to embrace these Swift concepts. I realized they were older concepts from other programming languages but wearing a fresh new coat that truly helped me become a better programmer and deliver better work. Over time, I started to love the Swift language and its pretty syntax. Since Swift 2, I had the luxury of working in a big company where we produced Swift code on a large scale, starting with about 20 developers and growing to over 40. After working with Swift with so many developers, and after being involved in hundreds of pull requests, I noticed that other developers had the same struggles as me. Or my fellow developers delivered code just fine, but didn’t realize that a more elegant or robust alternative was hidden, waiting to be discovered. Even though our code was correct, sometimes it could be a bit cleaner, or more succinct, or just a bit safer. I also noticed that we all stayed away from powerful techniques—such as generics or flatMap—because they were hard to grasp. Or we used to love the idea of generics, but weren’t sure why or when to apply it ourselves. After these realizations I started to write. First, these scribbles would be notes for myself on how to cleanly unwrap optionals, how lazy properties work, how to deal with generics, and so on. Then, these notes matured, and before I knew it I had enough content for some chapters. It was time to turn these notes into something more elaborate: a programming book that could help others shorten their Swift journey. With a few rough chapters in hand, I was wondering if I should throw an ebook online. However, with an “impressive” 200 people following me on Twitter and lacking a popular blog website, I figured I wouldn’t find the audience I wanted. Moreover, I thought that I had to learn a lot of the unknowns about writing a book. I decided to approach a publisher to help me turn these rough chapters into a great book. I approached Manning, and we’ve been working on this book together ever since. I believe these “small notes” have grown into something special. With the help of Manning and Swift friends, I have spent most of my free time for over a year writing and polishing and trying to make Swift’s tough concepts more simple to understand. By reading this book, I hope that it helps you on your path to becoming a Swift master. Also, I hope that you can tell I thoroughly enjoy sharing these concepts with you. I hope this book makes your Swift journey easy and fun. TJEERD IN ’T VEEN Acknowledgements Thank you, Manning, for helping me publish my first book. I want to give special thanks to Mike Stephens for taking the chance by getting me on board. Thank you, Helen Stergius, for working with me during the whole process. Thank you, Alain Couniot, for the great technical reviews of my chapters; it’s not an easy job to keep pointing out errors and possible improvements; still, I greatly appreciate it. And I also want to thank Alexander Pawlicki for his creative cartoon illustrations used throughout the book. Also thank you to the rest of the Manning team: Aleksandar Dragosavljević, Candace Gillhoolley, Ana Romac, Cheryl Weisman, Deirdre Hiam, Dottie Marsico, Nichole Beard, Mary Piergies, Carol Shields, Darren Meiss, Melody Dolab, and Marija Tudor. I want to give special thanks to friends, coworkers, and others who have been my guinea pigs and reviewed (parts of) the book: Bart den Hollander, Dimitar Gyurov, Dario de Rosa, Rein Spijkerman, Janina Kutyn, Sidney de Koning, Torben Schulz, and Edwin Chun Wing Kwok. Also, I would like to thank the other reviewers: Alessandro Campeis, Ali Naqvi, Axel Roest, David Jacobs, Gustavo Gomes, Helmut Reiterer, Jason Pike, John Montgomery, Kent R. Spillner, Lars Petersen, Marcelo Pires, Marco Giuseppe Salafia, Martin Philp, Monica Guimaraes, Patrick Regan, Tyler Slater, and Weyert de Boer. I knew writing a book was tough, but it was much harder than I imagined it to be. My fiancée Jenika and I just had a baby daughter, and it was quite the struggle to start a new family, have sleepless nights, maintain a full-time job, and write a programming book in a second language. I couldn’t have done it if I didn’t love writing this book while having the support of my fiancée. Thank you, Jenika, for being so patient with me. About this book Swift is a young language. At the time of writing, Swift has reached the fourth version and yet is not ABI-stable, meaning that there will be breaking changes when Swift 5 comes out. So why is this book in any position to tell you how to write your code? You’d be right to be skeptical, but please bear with me. Even though Swift is relatively new, I think it’s fair to say that some solutions work better than others, which is even more essential to understand if you’re using Swift for real production apps. Swift borrows a lot of important concepts from other programming languages, such as Haskell, Ruby, Rust, Python, C#, and others. Therefore, you’d be wise to keep an eye out for these concepts. By mixing programming paradigms with real-world experience, this book shares some very fun and useful best practices you can instantly apply to your work. Having programmed for over a decade in multiple languages and teams, I would like to share tips, tricks, and guidelines that helped my Swift career tremendously, and I want the same for you. WHY THIS BOOK? Honestly, a lot of software in this world runs on “ugly” code, and that is completely normal. If your product does what it needs to do, that is—like it or not—good enough for businesses. As a developer, you have to make sure your product works and works well. But your users won’t look under the hood and point out ugly if statements. Perfectionism is harmful to software development and the cause to large numbers of unfinished projects. Still, there’s a large gap between “It does what it needs to do” and a project where some excellent decisions were made that pay off in the long run. Having worked on numerous projects, one thing I highly value is writing code that your coworkers and your future self will understand clearly—because elegant code means less chance of bugs, higher maintainability, better understanding for developers who inherit code, increased programmer happiness, and many other benefits. Another aspect I value is the robustness of code, meaning how refactor-proof some pieces are. Will it break if you sneeze on it? Or can you change code without a hassle? In this book, I share my tips, tricks, and guidelines that have worked well for me and companies I’ve worked for. On top of that, it fills in significant knowledge gaps that may arise while working with Swift. Although this is a Swift book, a lot of the principles shared here are not Swift-centric and carry over to other programming languages as well; this is because Swift borrows a lot of ideas and paradigms from other languages. After you finish this book, you may find it easy to apply concepts in other languages. For instance, you’ll learn a lot about optionals, or how to use the reduce method on arrays. Later, you may decide to learn Kotlin, where you may apply optionals and reduce—called fold—straight away. You may also find Rust—and its similar generics implementation—easier to learn. Because of Swift’s multi-paradigm nature, this book switches without preference between object-oriented programming, functional programming, and protocol-oriented programming paradigms—although admittedly, I do favor other techniques over subclassing. Switching between these paradigms offers you many tools and solutions to a problem, with insights as to why a certain solution works well or not. Whether you’re stuck in a rut or open to many new programming insights, this book challenges you to solve problems in different ways . IS THIS BOOK FOR YOU? This book does assume that you have made one or more applications in Swift. Do you work in a team? Even better—this book shows you how to write good, clear code that gets appreciated in teams, and helps you improve pull requests of others. Your code will be more robust and cause less maintenance for you and your team. This book fills in knowledge gaps for both beginner and seasoned Swift developers. Perhaps you mastered protocols but still struggle with flatMapping on types or asynchronous error handling. Or maybe you create beautiful apps but stay away from generics because they can be hard to interpret. Or perhaps you sort-of know when to use a struct versus a class but aren’t aware that enums are sometimes a better alternative. Either way, this book helps you with these topics. By the end, generics should come as naturally as for loops. You’ll be confident calling flatMap on optionals, know how to work with associated types, and you’ll gladly use reduce in your daily routine when working with iterators. If you’re aiming to get a programming interview for a new job in the future, you’re in for a treat. You’re going to be able to answer a lot of relevant questions in regard to Swift development trade-offs and decisions. This book can even help you write elegant code in your code assignments. If you just want an app in the app store, just keep doing what you’re doing; no need to read this book! But if you want to write code that is more robust, easier to understand, and increases your chances of getting a job, getting better at your job, or giving qualitative comments on pull requests, you’re at the right place. WHAT THIS BOOK IS NOT This book is focused on Swift. It mostly uses framework-free examples because it isn’t about teaching Cocoa, iOS, Kitura, or other platforms and frameworks. What does happen in this book is I often make use of Apple’s Foundation, which is hard to avoid if you want real-world examples. If you’re on Linux, you can use swift.org’s Foundation alternative to get similar results. A BIG EMPHASIS ON PRACTICAL SCENARIOS This book is very practical, showcasing tips and tricks you can apply straight away in your daily programming. Don’t worry: it’s not a theory-dense book. You’ll learn a lot of theory, but only via the use of real- world problems that any Swift developer runs into sooner or later. It doesn’t, however, reach an academic level where it discusses Swift’s LLVM representation or machine code. Also, I made sure to avoid a personal pet peeve of mine: I do not subclass “Animal” with “Dog” or add a “Flyable” protocol to “Bird.” I also don’t add “Foo” to “Bar.” You’ll deal with real-world scenarios, such as talking to APIs, loading local data, and refactoring and creating functions, and you’ll see useful bits and pieces of code you can implement in your projects. ROADMAP The following sections provide an overview of the book, divided into chapters. The book is quite modular, and you can start with any chapter that interests you. Some chapters I consider crucial chapters. Chapter 4, “Making optionals second nature,” is key, because optionals are so prevalent in Swift and return over and over again in chapters. To understand the abstract side of Swift, I highly recommend reading chapter 7, “Generics,” chapter 8, “Putting the pro in protocol-oriented programming,” and chapter 12, “Protocol extensions.” Together, these chapters lay a solid foundation for key Swift skills. Be sure not to skip these! As a bonus, if you’re interested in learning functional programming techniques, direct your attention to chapter 2, “Modeling data with enums,” chapter 10, “Understanding map, flatMap, and compactMap,” and chapter 11, “Asynchronous error handling with Result.” Chapter 1: Introducing Swift in depth This warmup chapter shows the current state of Swift, what it’s good at, what it’s not so good at, and what you’ll be doing in this book. It’s not very technical, but it sets expectations and prepares you for what you’ll learn. Chapter 2: Modeling data with enums This chapter is excellent if you want to flex your brain and think differently about modeling data and see how far enums can go to help you.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.