iOS 14 Programming for Beginners Fifth Edition Get started with building iOS apps with Swift 5.3 and Xcode 12 Ahmad Sahar BIRMINGHAM—MUMBAI iOS 14 Programming for Beginners Fifth Edition Copyright © 2020 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 or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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. Commissioning Editor: Pavan Ramchandani Acquisition Editor: Ashitosh Gupta Senior Editor: Hayden Edwards Content Development Editor: Aamir Ahmed Technical Editor: Deepesh Patel Copy Editor: Safis Editing Project Coordinator: Kinjal Bari Proofreader: Safis Editing Indexer: Priyanka Dhadke Production Designer: Shankar Kalbhor First published: January 2017 Second Edition: January 2018 Third Edition: December 2018 Fourth Edition: January 2020 Fifth Edition: November 2020 Production reference: 2251120 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80020-974-9 www.packt.com To my beloved wife, Oni. Ahmad Sahar Packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals • Improve your learning with Skill Plans built especially for you • Get a free eBook or video every month • Fully searchable for easy access to vital information • Copy and paste, print, and bookmark content 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 packt.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.packt.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. Contributors About the author Ahmad Sahar is a trainer, presenter, and consultant at Tomafuwi Productions, specializing in conducting training courses for macOS and iOS, macOS Support Essentials certification courses, and iOS development courses. He is a member of the DevCon iOS and MyCocoaHeads online communities in Malaysia, and has conducted presentations and talks for both groups. In his spare time, he likes building and programming LEGO Mindstorms robots. About the reviewers Robin Douglas is an experienced software engineer specializing in native iOS development, and is a computer science graduate from the University of Leeds, UK. While at the UoL, he conducted research into iOS development for use in education. He is currently working on new native iOS apps for Sky Betting & Gaming. Some other large projects he has worked on recently are Odhealth (formally known as HealthPod), which facilitates virtual sessions with personal trainers, and Artuoso, a social network for artists. Tom Holmes is an iOS developer and software engineer with intense training in Swift 5 and iOS development practices, such as test-driven development, SOLID principles, MVC, MVVM, and frameworks both native to Apple and from third parties. He has a great interest in how software can be applied to the financial sector to make investing by analyzing stocks easier and more accessible to the masses, and how apps can help achieve this vision. He also has an interest in how machine learning and AI will shape the landscape of investing in the near future, and wishes to play a part in this future. He's very passionate about technology and wishes to build a great career in it. His other areas of expertise include Python 3, NumPy, JavaScript, SQL, and some web development. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors. packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface Section 1: Swift 1 Getting Familiar with Xcode Technical requirements 4 Running the app in the iOS Downloading and installing simulator 10 Xcode from the App Store 4 Using an iOS device for Understanding the Xcode user development 13 interface 8 Connecting an iOS device wirelessly 20 Summary 22 2 Simple Values and Types Technical requirements 24 Using common data types in the playground 31 Understanding Swift Playgrounds2 4 Customizing fonts and colors 27 Exploring constants and Running playground code 28 variables 32 Understanding type inference Exploring data types 29 and type safety 34 Representing integers 29 Using type annotation to specify a type 35 Representing floating-point numbers 29 Using type safety to check values 36 Representing Booleans 30 Representing strings 30 Exploring operators 36 Using arithmetic operators 37 Performing string operations 41 Using compound assignment operators 38 Using the print() instruction 42 Using comparison operators 39 Summary 43 Using logical operators 40 3 Conditionals and Optionals Technical requirements 46 Introducing optionals 50 Introducing conditionals 46 Using optionals and optional binding 51 Using if statements 47 Summary 55 Using switch statements 49 4 Range Operators and Loops Technical requirements 58 Using the while loop 62 Exploring range operators 58 The repeat-while loop 62 Exploring loops 60 Summary 63 Using the for-in loop 60 5 Collection Types Technical requirements 65 Checking the number of elements in a dictionary 71 Understanding arrays 66 Adding a new element to a dictionary 72 Creating an array 67 Accessing a dictionary element 72 Checking the number of elements in Assigning a new value to an existing key 73 an array 67 Removing an element from a dictionary 73 Adding a new element to an array 68 Iterating over a dictionary 74 Accessing an array element 68 Assigning a new value to a particular Understanding sets 74 index 69 Creating a set 75 Removing an element from an array 69 Checking the number of elements in a Iterating over an array 70 set 75 Understanding dictionaries 70 Adding a new element to a set 76 Creating a dictionary 71 Checking whether a set contains an Exploring set operations 77 element 76 Exploring set membership and equality 78 Removing an item from a set 76 Summary 79 Iterating over a set 77 6 Functions and Closures Technical requirements 82 Using a guard statement to exit a function early 87 Understanding functions 82 Creating a function 83 Understanding closures 88 Using custom argument labels 84 Simplifying closures 89 Using nested functions 84 Summary 90 Using functions as return types 85 Using functions as parameters 86 7 Classes, Structures, and Enumerations Technical requirements 92 Comparing value types and references types 101 Understanding classes 92 Deciding between classes and Creating a class declaration 93 structures 103 Making an instance of the class 94 Making a subclass 96 Understanding enumerations 103 Overriding a superclass method 97 Creating an enumeration 104 Understanding structures 99 Summary 105 Creating a structure declaration 100 Making an instance of the structure 101 8 Protocols, Extensions, and Error Handling Technical requirements 108 Understanding extensions 111 Understanding protocols 108 Adopting a protocol via an extension 112 Creating a protocol declaration 110