Structs 42 Getting Ready 43 How to Do It
Total Page:16
File Type:pdf, Size:1020Kb
Swift 4 Programming Cookbook 50 task-oriented recipes to make you productive with Swift 4 Keith Moon BIRMINGHAM - MUMBAI Swift 4 Programming Cookbook Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: September 2017 Production reference: 1260917 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-089-9 www.packtpub.com Credits Author Copy Editor Keith Moon Shaila Kusanale Reviewer Project Coordinator Giordano Scalzo Ulhas Kambali Commissioning Editor Proofreader Ashwin Nair Safis Editing Acquisition Editor Indexer Larissa Pinto Mariammal Chettiyar Content Development Editor Graphics Onkar Wani Abhinash Sahu Technical Editor Production Coordinator Akhil Nair Shraddha Falebhai About the Author Keith Moon is an award-winning iOS developer, author, and speaker based in London. He has worked with some of the biggest companies in the world to create engaging and personal mobile experiences. Keith has been developing in Swift since its release, working on projects both fully Swift, and mixed Swift and Objective-C. Keith has been invited to speak about Swift development in conferences from Moscow to Minsk and London. I would like to thank my amazing and supportive wife, Alissa. Without her support and patience, this book would not have been possible. About the Reviewer Giordano Scalzo is a developer with 20 years of programming experience, since the days of the ZXSpectrum. He has worked in C++, Java, .NET, Ruby, Python, and in a ton of other languages that he has forgotten the names of. After years of backend development, over the past five years, Giordano has developed extensively for iOS, releasing more than 20 apps--apps that he wrote for clients, enterprise applications, or on his own. Currently he is a contractor in London, where through his company Effective Code Ltd, http://effectivecode.co.uk, he delivers code for iOS aiming at quality and reliability. In his spare time, when he is not crafting retro game clones for iOS, he writes his thoughts on http://giordanoscalzo.com. I’d like to thank my better half Valentina, who lovingly supports me in everything I do; without you, none of this would have been possible. Thanks to my bright future, Mattia and Luca, for giving me lots of smiles and hugs when I needed them. Finally, my gratitude goes to my mum and my dad, who gave me curiosity and the support to follow my passions, which began one day when they bought me a ZXSpectrum. www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1786460890. If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products! Table of Contents Preface 1 Chapter 1: Swift Building Blocks 7 Introduction 7 Your first Swift program 8 Getting ready 9 Downloading Xcode 9 How to do it... 13 There's more... 17 See also 18 Strings, Ints, Floats, and Bools 18 Getting ready 19 How to do it... 19 How it works... 21 There's more... 24 See also 25 Optionals, unwrap, and force unwrap 25 Getting started 26 How to do it... 26 How it works... 27 There's more... 29 See also 31 Functions 31 How to do it... 31 How it works... 32 There's more... 34 Default parameter values 34 Parameter overloading 35 See also 36 Object classes 36 Getting ready 36 How to do it... 36 How it works... 37 There's more... 41 See also 42 Structs 42 Getting ready 43 How to do it... 43 How it works... 43 There's more... 44 See also 45 Enumerations 46 Getting ready 46 How to do it... 47 How it works... 47 There's more... 49 Methods and computed variables 49 Associated values 50 See also 51 Closures 51 Getting ready 52 How to do it... 52 How it works... 54 There's more... 56 See also 57 Protocols 58 Getting ready 58 How to do it... 58 How it works... 58 There's more... 60 See also 61 Chapter 2: Building on the Building Blocks 62 Introduction 62 Bundling variables into tuples 63 Getting ready 63 How to do it... 63 How it works... 64 There's more... 64 See also 65 Ordering your data with arrays 66 How to do it... 66 How it works... 68 There's more... 71 See also 72 Containing your data with sets 72 [ ii ] How to do it... 72 How it works... 73 Union 74 Intersection 74 Symmetric difference 75 Subtracting 76 There's more... 76 See also 80 Storing key-value pairs with Dictionaries 80 Getting ready 80 How to do it... 81 How it works... 82 There's more... 84 See also 86 Subscripts for custom types 86 Getting ready 86 How to do it... 87 How it works... 90 There's more... 91 See also 92 Changing your name with typealias 92 How to do it... 92 There's more... 93 See also 95 Getting property changing notifications using property observers 95 Getting ready 95 How to do it... 95 How it works... 96 There's more... 97 See also 97 Controlling access with access control 97 Getting ready 98 How to do it... 103 How it works... 106 There's more... 110 See also 111 Extending functionality with extensions 111 Getting ready 111 How to do it... 112 How it works... 112 [ iii ] There's more... 113 See also 115 Chapter 3: Data Wrangling with Swift Control Flow 116 Introduction 116 Making decisions with if/else 116 Getting ready 117 How to do it... 117 How it works... 118 There's more... 119 Conditional unwrap 119 Chain unwrapping Optionals 121 Enum with associated values 123 See also 124 Switch it up 124 Getting ready 125 How to do it... 125 How it works... 127 See also 130 For the love of loops 131 How to do it... 131 How it works... 132 See also 133 While loops 133 Getting ready 133 How to do it... 134 How it works... 134 There's more... 135 See also 135 Try, throw, do and, catch - Swift error handling 136 How to do it... 136 How it works... 138 There's more... 139 See also 143 Checking up front with guard 144 How to do it... 144 How it works... 146 See also 147 Doing it later with defer 147 Getting ready 148 [ iv ] How to do it... 149 How it works... 150 There's more... 151 See also 153 Bailing out with fatalError and precondition 153 Getting ready 153 How to do it... 154 How it works... 155 See also 156 Chapter 4: Generics, Operators, and Nested Types 157 Introduction 157 Using generics with types 158 How to do it... 159 How it works... 161 There's more... 162 See also 164 Using generics with functions 164 How to do it... 164 How it works... 165 There's more... 166 See also 167 Using generics with protocols 167 Getting ready 168 How to do it... 168 There's more... 172 See also 173 Advanced operators 174 Getting ready 174 How to do it... 174 See also 177 Option set 178 How to do it... 178 How it works... 179 See also 179 Create custom operators 179 Getting ready 180 How to do it... 180 How it works... 183 There's more... 184 [ v ] See also 186 Nested types 186 Getting ready 187 How to do it... 187 How it works..