
Swift 4 Protocol-Oriented Programming Third Edition Bring predictability, performance, and productivity to your Swift applications Jon Hoffman BIRMINGHAM - MUMBAI Swift 4 Protocol-Oriented Programming Third Edition 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: February 2016 Second edition: November 2016 Third edition: October 2017 Production reference: 1031017 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78847-003-2 www.packtpub.com Credits Author Copy Editor Jon Hoffman Safis Editing Reviewer Project Coordinator Andrea Prearo Ulhas Kambali Commissioning Editor Proofreader Kunal Chaudhari Safis Editing Acquisition Editor Indexer Reshma Raman Aishwarya Gangawane Content Development Editor Graphics Vikas Tiwari Abhinash Sahu Technical Editor Production Coordinator Subhalaxmi Nadar Aparna Bhagat About the Author Jon Hoffman has over 25 years of experience in the field of information technology. Over these 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 developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon’s other interests are spending time with his family, robotic projects, and 3D printing. 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 Kara is currently working towards her black belt. About the Reviewer Andrea Prearo is a software engineer with over 15 years of experience. He is originally from Italy, and after a decade of writing software in C/C++ and C#, he moved to the Bay Area in 2011 to start developing mobile apps. In the last few years, he has been focusing on Swift, Objective-C, iOS, and microservices, with some short explorations of the Android platform. Currently, he is a member of the iOS dev team at Capital One, working on the company's flagship mobile banking app. His interests include reading books, watching movies, and hiking. From time to time, he also blogs about tech at https://medium.com/@andrea.prearo. I would like to thank my wonderful wife, Nicole, for her never-ending support in all my endeavors. 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/1788470036. If you'd like to join our team of regular reviewers, you can email 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 What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Downloading the color images of this book Errata Piracy Questions 1. Starting with the Protocol Protocol syntax Defining a protocol Property requirements Method requirements Optional requirements Protocol inheritance Protocol composition Using protocols as a type Polymorphism with protocols Type casting with protocols Associated types with protocols Delegation Designing with protocols Protocols in the Swift standard library Summary 2. Our Type Choices Classes Structures Access controls Enumerations Tuples Protocols Value and reference types Recursive data types for reference types only Inheritance for reference types only Dynamic dispatch Swift's built-in types Copy-on-write Summary 3. Extensions Defining an extension Protocol extensions Text validation Extensions with the Swift standard library Conforming to the Equatable protocol Summary 4. Generics Generic functions Type constraints with Generics Generic types Associated types Generic subscripts Copy-on-write Generics in a protocol-oriented design Generics in the Swift standard library Summary 5. Object-Oriented Programming What is object-oriented programming? Requirements for the sample code Swift as an object-oriented programming language Issues with the object-oriented design Summary 6. Protocol-Oriented Programming Requirements for the sample code Swift as a protocol-oriented programming language Summarizing protocol-oriented programming and object-oriented programming Differences between object-oriented programming and protocol-oriented programming Protocol and protocol extensions compared with superclasses Implementing vehicle types Using value and reference types The winner is... Summary 7. Adopting Design Patterns in Swift What are design patterns? Creational patterns The singleton design pattern Understanding the problem Understanding the solution Implementing the singleton pattern The builder design pattern Understanding the problem Understanding the solution Implementing the builder pattern The factory method pattern Understanding the problem Understanding the solution Implementing the factory method pattern Structural design patterns The bridge pattern Understanding the problem Understanding the solution Implementing the bridge pattern The facade pattern Understanding the problem Understanding the solution Implementing the facade pattern The proxy design pattern Understanding the problem Understanding the solution Implementing the proxy pattern Behavioral design patterns The command design pattern Understanding the problem Understanding the solution Implementing the command pattern The strategy pattern Understanding the problem Understanding the solution Implementing the strategy pattern The observer pattern Understanding the problem Understanding the solution Implementing the observer pattern Summary 8. Case Studies Logging service Requirements The design Conclusion Data access layer Requirements The design Data model layer Data helper layer Bridge layer Using the data access layer Conclusion Summary Preface This book is about protocol-oriented programming. When Apple announced Swift 2 at the World Wide Developers Conference (WWDC) in 2015, they also declared that Swift was the world's first protocol-oriented programming language. By its name, we may assume that protocol-oriented programming is all about the protocol, however, that would be a wrong assumption. Protocol-oriented programming is about so much more than just the protocol; it is actually a new way of not only writing applications but also how we think about programming. In the first four chapters, we take an in-depth look at each of the components of the protocol-oriented programming paradigm. These chapters are designed to give the reader a solid understanding of the different components of protocol-oriented programming, so he/she will understand how they can use these components in their applications. One of the biggest misconceptions about protocol-oriented programming is that it is just another name for object-oriented programming. In Chapter 5, Object-Oriented Programming and Chapter 6, Protocol-Oriented Programming we take on this myth by comparing protocol-oriented programming to object-oriented programming to see what is similar and what is different. We also discuss
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages264 Page
-
File Size-