O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Swift 4 Protocol-Oriented Programming - Third Edition

Book Description

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

About This Book

  • Leverage the power of protocol-oriented programming in your applications and learn from real-world use cases
  • Create a flexible code base with protocols and protocol extensions
  • Leverage the power of generics in Swift 4 to create very flexible frameworks

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

  • Understand the differences between object-oriented programming and protocol-oriented programming
  • Explore the different types that Swift offers and what pitfalls to avoid
  • Delve into generics and generic programming
  • Learn how to implement Copy-On-Write within your custom types
  • Implement several design patterns in a protocol-oriented way
  • Design applications by prioritizing the protocol first and the implementation types second

In Detail

Swift has become the number one language used in iOS and macOS development. The Swift standard library is developed using protocol-oriented programming techniques, generics, and first-class value semantics; therefore, every Swift developer should understand these powerful concepts and how to take advantage of them in their application design.

This book will help you understand the differences between object-oriented programming and protocol-oriented programming. It will demonstrate how to work with protocol-oriented programming using real-world use cases. You will gain a solid knowledge of the various types that can be used in Swift and the differences between value and reference types. You will be taught how protocol-oriented programming techniques can be used to develop very flexible and easy-to-maintain code.

By the end of the book, you will have a thorough understanding of protocol-oriented programming and how to utilize it to build powerful and practical applications.

Style and approach

This book is written for developers who learn best by working with code, so every concept discussed in this book is reinforced with real code examples.

Downloading the example code for this book. 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 code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Starting with the Protocol
    1. Protocol syntax
      1. Defining a protocol
      2. Property requirements
      3. Method requirements
      4. Optional requirements
    2. Protocol inheritance
    3. Protocol composition
    4. Using protocols as a type
    5. Polymorphism with protocols
    6. Type casting with protocols
    7. Associated types with protocols
    8. Delegation
    9. Designing with protocols
    10. Protocols in the Swift standard library
    11. Summary
  3. Our Type Choices
    1. Classes
    2. Structures
    3. Access controls
    4. Enumerations
    5. Tuples
    6. Protocols
    7. Value and reference types
    8. Recursive data types for reference types only
    9. Inheritance for reference types only
    10. Dynamic dispatch
    11. Swift's built-in types
    12. Copy-on-write
    13. Summary
  4. Extensions
    1. Defining an extension
    2. Protocol extensions
    3. Text validation
    4. Extensions with the Swift standard library
    5. Conforming to the Equatable protocol
    6. Summary
  5. Generics
    1. Generic functions
    2. Type constraints with Generics
    3. Generic types
    4. Associated types
    5. Generic subscripts
    6. Copy-on-write
    7. Generics in a protocol-oriented design
    8. Generics in the Swift standard library
    9. Summary
  6. Object-Oriented Programming
    1. What is object-oriented programming?
    2. Requirements for the sample code
    3. Swift as an object-oriented programming language
    4. Issues with the object-oriented design
    5. Summary
  7. Protocol-Oriented Programming
    1. Requirements for the sample code
    2. Swift as a protocol-oriented programming language
    3. Summarizing protocol-oriented programming and object-oriented programming
    4. Differences between object-oriented programming and protocol-oriented programming
      1. Protocol and protocol extensions compared with superclasses
      2. Implementing vehicle types
      3. Using value and reference types
    5. The winner is...
    6. Summary
  8. Adopting Design Patterns in Swift
    1. What are design patterns?
    2. Creational patterns
      1. The singleton design pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the singleton pattern
      2. The builder design pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the builder pattern
      3. The factory method pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the factory method pattern
    3. Structural design patterns
      1. The bridge pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the bridge pattern
      2. The facade pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the facade pattern
      3. The proxy design pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the proxy pattern
    4. Behavioral design patterns
      1. The command design pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the command pattern
      2. The strategy pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the strategy pattern
      3. The observer pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the observer pattern
    5. Summary
  9. Case Studies
    1. Logging service
      1. Requirements
      2. The design
      3. Conclusion
    2. Data access layer
      1. Requirements
      2. The design
      3. Data model layer
      4. Data helper layer
      5. Bridge layer
      6. Using the data access layer
      7. Conclusion
    3. Summary