Protocol-Oriented Programming with Swift - Jon Hoffman - E-Book

Protocol-Oriented Programming with Swift E-Book

Jon Hoffman

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Build fast and powerful applications by exploiting the power of protocol-oriented programming in Swift

About This Book

  • The only book that shows how to harness the power of Protocol-Oriented Programming in Swift to build real-world applications,
  • Get familiar with the protocol focused approach of application development,
  • Increase the overall productivity and performance of applications with Protocol Oriented Programming.

Who This Book Is For

This book is for Swift developers who want to learn and implement protocol oriented programming in their real world applications.

What You Will Learn

  • The difference between Object-Oriented programming and Protocol-Oriented programming
  • The difference between reference and value types and when to use each
  • How we can leverage tuples to reduce the complexity of our code
  • What are protocols and how to use them
  • How to implement protocol extensions to create a very flexible code base
  • How to implement several design patterns in a Protocol-Oriented approach
  • How to solve real world design issue with protocol oriented programming

In Detail

At the heart of Swift's design is an incredibly powerful idea: protocol-oriented programming. Its many benefits include better code maintainability, increased developer productivity and superior application performance. The book will teach the reader how to apply the ideas behind the protocol oriented programing paradigm to improve the code they write.

This book will introduce the readers to the world of protocol-oriented programming in Swift and will demonstrate the ideas behind this new programming paradigm with real world examples. In addition to learning the concepts of Protocol Oriented programming, it also shows the reader how to reduce the complexity of their codebase using protocol extensions. Beginning with how to create simple protocols, readers will learn how to extend protocols and also to assign behaviors to them.

By the end of this book readers will be able to harness the power of protocol-oriented programming to build real world applications.

Style and approach

In its latest release of Swift, Apple has introduced Protocol Extensions as a new feature at the heart of Swifts design making Swift 2 a protocol-oriented language. Protocol oriented programming being a less explored OOP paradigm, there is little guidance on hot to take advantage of protocol extensions in real-world applications. In addition to offering an in-depth coverage of protocol oriented programming and its concepts, this book also explains how a developer can leverage these features to build powerful, real-world applications

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 293

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Protocol-Oriented Programming with Swift
Credits
About the Author
About the Reviewer
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
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
Errata
Piracy
Questions
1. Object-Oriented and Protocol-Oriented Programming
Swift as an object-oriented programming language
Swift as a protocol-oriented programming language
Summarizing protocol-oriented programming and object-oriented programming
Object-oriented programming versus protocol-oriented programming
Protocol and protocol extensions versus superclasses
Implementing drink types
Value types vs reference types
The winner is...
Summary
2. Our Type Choices
The class
The structure
The enumerations
The tuple
Protocols
Value versus class types
Recursive data types (reference types only)
Inheritance (reference types only)
Swift built-in data types and data structures
Summary
3. Catching Our Errors
Using the guard statement
Error handling
Error handling with return values
Error handling with NSError
Error handling with Swift 2
Representing errors
Throwing errors
Catching errors
When to use error handling
Summary
4. All about 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 and developing with protocols
Summary
5. Let's Extend Some Types
Defining an extension
Protocol extensions
Text validation
Summary
6. 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 façade pattern
Understanding the problem
Understanding the solution
Implementing the façade 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
7. Case Studies
Logging service
Requirements
The design
Conclusion
Data access layer with SQLite.swift
Requirements
The design
Connection layer
Data model layer
Data helper layer
Conclusion
Calculator application
Requirements
The design
Conclusion
Summary
Index

Protocol-Oriented Programming with Swift

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

Jon Hoffman

Reviewer

Chuck Gaffney

Commissioning Editor

Wilson D'souza

Acquisition Editor

Tushar Gupta

Content Development Editor

Sumeet Sawant

Technical Editor

Jayesh Sonawane

Copy Editors

Stephen Copestake

Akshata Lobo

Project Coordinator

Dinesh Rathe

Proofreader

Safis Editing

Indexer

Monica Ajmera Mehta

Graphics

Disha Haria

Production Coordinator

Nilesh Mohite

Cover Work

Nilesh Mohite

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.

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 PacktCopy and paste, print, and bookmark contentOn 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.

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 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 thinking about programming as well.

One of the biggest misconceptions about protocol-oriented programming is that it is just another name for object-oriented programming. Chapter 1, Object-Oriented and Protocol-Oriented Programming, immediately takes on this myth by comparing protocol-oriented programming to object-oriented programming to see what is similar and what the differences are. We will also discuss the advantages and disadvantages of both programming paradigms.

From Chapter 2, Our Type Choices, to Chapter 5, Let's Extend Some Types, we will 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 users will understand how they can use these components in their applications.

The last two chapters are written to help the reader understand how they can design their application in a protocol-oriented programming way. Chapter 6, Adopting Design Patterns in Swift, looks at how we would implement several design patterns in a protocol-oriented way and Chapter 7, Case Studies, looks at three real-world case studies to reinforce everything previously discussed in the book.

What this book covers

Chapter 1, Object-Oriented and Protocol-Oriented Programming, will show the differences between object-oriented programming and protocol-oriented programming, giving the reader a good understanding of the different paradigms. By the end of the chapter, the reader should have a basic understanding of protocol-oriented programming and what it offers.

Chapter 2, Our Type Choices, will look at the different types that Swift offers (structs, classes, enums, and tuples). We will show several examples of when to use the various types and when not to.

Chapter 3, Catching Our Errors, will look at the various ways we can catch and report errors. This chapter will not only cover the new Swift 2 do-try-catch blocks, but also the older error handling patterns.

Chapter 4, All about the Protocol, will be all about the protocol. We will show the advantages of using protocols and why, in our design, we should start with the protocol.

Chapter 5, Let's Extend Some Types, will cover extensions in great detail. We will look at how to extend standard Swift types, such as structures and classes. We will also look at protocol extensions and discuss why they are so important in protocol-oriented programming.

Chapter 6, Adopting Design Patterns in Swift, will be implementing several design patterns using protocol-oriented programming. For each of the design patterns, we will look at the problem they are designed to solve and how to implement the pattern.

Chapter 7, Case Studies, will explore three case studies. This chapter is designed to pull everything from the first six chapters together to show the reader how to use protocol-oriented programming with design patterns in real-world situations.

What you need for this book

To follow along with the examples in this book, the reader will need to have an Apple computer with OS X 10.10 or higher installed. They will also need to install Xcode version 7.0 or higher with Swift version 2 or higher and have at least basic knowledge of the Swift programming language.

The examples, as they are shown in this book, should work with the Linux port of the Swift language however the downloadable code for this book requires Xcode which is only available on the Apple platform.

Who this book is for

This book is for Swift developers who have at least an introductory knowledge of the Swift programming language, who want to learn and implement protocol-oriented programming in their real-world applications. It is also for developers who not only want to understand protocol-oriented programming, but also want to fully understand the different components of the programming paradigm. This book is written for developers who learn best by looking at and working with code, because every concept covered in the book is backed by example code written to give the reader a solid understanding of the current topic and to demonstrate how to properly implement it.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

class Jolt { var volume: Double var caffeine: Double var temperature: Double var canSize: Double var description: String

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The answer to this question is polymorphism.".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.