ebook img

Protocol Oriented Programming with Swift PDF

212 Pages·2016·1.257 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 Protocol Oriented Programming with Swift

[ 1 ] Protocol-Oriented Programming with Swift Build fast and powerful applications by exploiting the power of protocol-oriented programming in Swift Jon Hoffman BIRMINGHAM - MUMBAI Protocol-Oriented Programming with Swift Copyright © 2016 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: February 2016 Production reference: 1190216 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78588-294-4 www.packtpub.com Credits Author Project Coordinator Jon Hoffman Dinesh Rathe Reviewer Proofreader Chuck Gaffney Safis Editing Commissioning Editor Indexer Wilson D'souza Monica Ajmera Mehta Acquisition Editor Graphics Tushar Gupta Disha Haria Content Development Editor Production Coordinator Sumeet Sawant Nilesh Mohite Technical Editor Cover Work Jayesh Sonawane Nilesh Mohite Copy Editors Stephen Copestake Akshata Lobo About the Author Jon Hoffman has over 20 years of experience in the field of information technology. Over these 20 years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has been developing extensively for the iOS platform since 2008. This includes several apps that he has published in the app store—apps that he wrote for third parties and numerous enterprise applications. Packt Publishing published Jon's first book, iOS and OS X Network Programming, in January 2014. What really drives Jon is the challenges in the Information Technology field and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are watching baseball (go Sox) and basketball (go Celtics). Jon also really enjoys tae kwon do where he and his oldest daughter Kailey earned their black belts together early in 2014. Kim (his wife) earned her black belt in December 2014 and his youngest daughter is currently working towards her black belt. I would like to thank my wonderful wife Kim, without whose support, encouragement, patience, and understanding, this book would never have been written. I would also like to thank my two wonderful daughters Kailey and Kara who have both been my inspiration and driving force since the days they were born. About the Reviewer Chuck Gaffney is the owner of the website/store and indie studio Chuck's Anime Shrine as well as Anime.FM. He is a programmer by trade, ranging from games, non-games, VR, AR, and mobile applications. He has worked with iOS native (Swift/Objective-C/XCode) code, Flash animation, and game development as well as web design. Recently, he has been working almost exclusively in Unity/C#, making mobile applications that work with games, non-games, AR, VR, and third- party API integrations. Interestingly, he is also a professional voice actor with work in games, museum experiences, movie dubs, and more. He is the owner of the long running fan site, store, and indie game studio Chuck's Anime Shrine. He recently worked with two major NYC studios and with clients that include some financial giants and the Late Show with Steven Colbert. He is currently developing an app for a major beer company. I would like to thank my fiancée Danielle for putting up with the late nights and all nighters that come with the territory of being a programmer, voice actor, and business owner. www.PacktPub.com Support files, eBooks, discount offers, and more 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. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. 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 Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface v Chapter 1: Object-Oriented and Protocol-Oriented Programming 1 Swift as an object-oriented programming language 2 Swift as a protocol-oriented programming language 13 Summarizing protocol-oriented programming and object-oriented programming 18 Object-oriented programming versus protocol-oriented programming 19 Protocol and protocol extensions versus superclasses 19 Implementing drink types 22 Value types vs reference types 24 The winner is... 25 Summary 26 Chapter 2: Our Type Choices 27 The class 28 The structure 30 The enumerations 31 The tuple 36 Protocols 37 Value versus class types 38 Recursive data types (reference types only) 44 Inheritance (reference types only) 46 Swift built-in data types and data structures 49 Summary 50 Chapter 3: Catching Our Errors 51 Using the guard statement 52 Error handling 55 Error handling with return values 56 [ i ] Table of Contents Error handling with NSError 58 Error handling with Swift 2 61 Representing errors 62 Throwing errors 63 Catching errors 67 When to use error handling 70 Summary 72 Chapter 4: All about the Protocol 73 Protocol syntax 75 Defining a protocol 75 Property requirements 76 Method requirements 76 Optional requirements 77 Protocol inheritance 78 Protocol composition 79 Using protocols as a type 81 Polymorphism with protocols 83 Type casting with protocols 84 Associated types with protocols 85 Delegation 87 Designing and developing with protocols 90 Summary 95 Chapter 5: Let's Extend Some Types 97 Defining an extension 99 Protocol extensions 101 Text validation 106 Summary 114 Chapter 6: Adopting Design Patterns in Swift 115 What are design patterns? 116 Creational patterns 117 The singleton design pattern 118 Understanding the problem 118 Understanding the solution 119 Implementing the singleton pattern 119 The builder design pattern 121 Understanding the problem 121 Understanding the solution 121 Implementing the builder pattern 122 The factory method pattern 127 Understanding the problem 127 [ ii ] Table of Contents Understanding the solution 127 Implementing the factory method pattern 128 Structural design patterns 131 The bridge pattern 131 Understanding the problem 131 Understanding the solution 132 Implementing the bridge pattern 132 The façade pattern 136 Understanding the problem 136 Understanding the solution 136 Implementing the façade pattern 137 The proxy design pattern 139 Understanding the problem 139 Understanding the solution 140 Implementing the proxy pattern 140 Behavioral design patterns 142 The command design pattern 143 Understanding the problem 143 Understanding the solution 143 Implementing the command pattern 143 The strategy pattern 146 Understanding the problem 146 Understanding the solution 146 Implementing the strategy pattern 146 The observer pattern 148 Understanding the problem 148 Understanding the solution 148 Implementing the observer pattern 149 Summary 154 Chapter 7: Case Studies 155 Logging service 156 Requirements 157 The design 157 Conclusion 166 Data access layer with SQLite.swift 166 Requirements 167 The design 168 Connection layer 169 Data model layer 171 Data helper layer 172 Conclusion 181 Calculator application 181 Requirements 182 [ iii ]

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.