Title Page Copyright and Credits Hands-On Design Patterns with Swift About Packt Why subscribe? Packt.com Contributors About the authors About the reviewers Packt is searching for authors like you Preface Who this book is for What this book covers To get the most out of this book Download the example code files Download the color images Conventions used Get in touch Reviews Refreshing the Basics Classes and structs Classes Struct Enums Simple enums Adding methods Associating values Generic enums Raw type enums Switching the state of light Closures, functions, and currying Currying Using closures as callbacks Using weak and unowned Protocols Declaring a protocol Conforming to a protocol Conformance at declaration Conformance in an extension Protocol extensions Default implementations Tuples, type aliases, and generics Tuples Declaring tuples Destructuring tuples Using tuples in functions Type aliases Generics Generic functions Generic types Generics, protocols, and associated types Summary Understanding ARC and Memory Management A brief history of reference counting The semantics of reference counting Retain Release Assign Copying Using and misusing manual reference counting Memory leaks Dangling pointers ARC – what is that? Value types Strong references Weak references Unowned references Memory debugging Configuring your project Using the memory graph hierarchy tool Leaks, cycles, and dangling references Leaking with cycles A simple leak Fixing the leak Using weak Using unowned Dangling references Summary Diving into Foundation and the Standard Library Swift basic types Working with ranges Range as Sequence Throwing and catching errors Container types Arrays Mutability and operations Iterating, mapping, and reducing Dictionaries Initialization and mutability Iterating, mapping, and reducing Mastering concurrency with Dispatch Tasks and queues Synchronization with Dispatch Thread safety through serial queues Organizing execution with groups and semaphores Example of a counting semaphore Using groups HTTP with URLSession Making your first call with URLSession Parsing responses with Decodable Sending requests with Encodable Summary Working with Objective-C in a Mixed Code Base Setting up your project Importing Objective-C in Swift Exposing Swift to Objective-C Nullability and optionals in Objective-C Using NS_ASSUME_NON_NULL_BEGIN and NS_ASSSUME_NON_NULL_END Using nullable, nonnull, _Nullable, and _Nonnull Naming, renaming, and refining Objective-C for Swift Setting Objective-C names from Swift Setting Swift names from Objective-C Renaming classes Renaming methods and enum cases Lightweight generics in Objective-C Using typed NSArray* in Objective-C Generic classes in Objective-C Cocoa design patterns in Swift Delegation Using delegation Implementing delegation  Lazy initialization Summary Creational Patterns The singleton pattern  Using singletons Singletons in a nutshell The factory method pattern Using the factory method pattern Advanced usage of the factory method pattern Wrapping up The abstract factory pattern Using the abstract factory pattern Going further with factory methods Default implementations Inheritance Protocol extensions Checklist for using the factory method pattern The builder pattern Model building Going further: metaprogramming with Sourcery The builder pattern in a nutshell The prototype pattern Leveraging the prototype pattern Going further – NSCopying with Sourcery Implementing NSCopying automatically Implementing mutable objects Implementing NSMutableCopying automatically The prototype pattern in a nutshell Summary Structural Patterns The adapter pattern Using the adapter pattern The basics The classes to adapt Using classes as adapters Leveraging extensions The adapter pattern in a nutshell The decorator pattern Using a decorator  Going further with decorator Decoration in a nutshell The facade pattern and proxy pattern The facade pattern Building a network cache with the facade pattern Using the proxy pattern to implement request/response logging The composite pattern Using the composite pattern to represent tests and suites The bridge pattern Anatomy of the bridge pattern Using the bridge pattern The flyweight pattern A shopping list using the flyweight pattern Summary Behavioral Patterns The state pattern The card reader Using enums Refactoring for maintainability Extracting a single protocol Implementing all states through structs, and moving the logic Refactoring the context object Using state machines The observer pattern Event-based programming Using NotificationCenter Using Key-Value Observing Using KVO with existing Objective-C APIs Using KVO with Swift Observation using pure Swift Using observation The memento pattern Components of the memento pattern Implementing the memento pattern Using the memento pattern The visitor pattern Visitable and visitor protocols Contributors, thank you notes, and the visitor pattern Using visitors The strategy pattern Components of the strategy pattern The ice-cream shop example Using the strategy pattern Summary Swift-Oriented Patterns Getting started with protocol-oriented programming A refresher on protocols Adding requirements to protocols  Mutation and value types Protocols are full-fledged types Generics, conditional conformance, and associated types Generics-based programming Generic functions Generic everything Conditional conformance Associated types A word on Self requirement Protocol-oriented programming The type erasure pattern Elements of type erasure Closure-based type erasure Boxing-based type erasure The abstract base class The private box The public wrapper The type erasure pattern – a summary Template pattern with protocol-oriented programming A recommendation engine Summing up with the template method pattern Summary Using the Model-View-Controller Pattern A refresher on MVC The theory behind the MVC pattern A pure MVC example The model layer The view layer The controller layer UIViewController View controller life cycles UIViewController anti-patterns Early view instantiation Early view access in initializer Early view access in properties Composition and child view controllers Adding child view controllers Removing child view controllers Using view controller composition The model layer Using model controllers Refactoring controllers View controllers Model controllers The Controller Summary Model-View-ViewModel in Swift Basics of the MVVM pattern Refactoring MVC into MVVM Model ViewModel View Benefits and drawbacks of MVVM Enhanced testing Improved reusability Drawbacks MVVM and data binding Implementing the Observable class Implementing the Binding protocol Two-way binding on UITextField Using Observables with ViewModels Summary Implementing Dependency Injection Dependency Injection, a primer What is Dependency Injection? Definition Why DI is useful Separation of concerns Testability Dependency Injection by example Four ways to use Dependency Injection (with examples) Constructor Injection Property Injection Method Injection Ambient Context Bind the dependencies Composition Root DI anti-patterns Control Freak Stable and volatile dependencies Bastard Injection Service Locator Using a Dependency Injection Container Why you should use a DI Container The Typhoon framework Swinject Automatic Storyboard Injection Summary Futures, Promises, and Reactive Programming Callbacks and closures Closures and memory management The issue with callbacks Futures and promises Futures and promises under the hood Futures and promises frameworks for Swift PromiseKit Google Promises Reactive programming RxSwift Observables and observers Transformations Schedulers Asynchronous networking – an example Summary Modularize Your Apps with Swift Package Manager Creating a library package Adding features to the library Adding more targets Adding third-party dependencies Using SPM with Xcode Extracting and sharing a framework Refactoring your code Extracting a framework Summary Testing Your Code with Unit and UI Tests Unit testing using XCTest Testing an RPN Calculator app TDD What is an reverse polish notation calculator? A simple RPN Calculator app The first test More tests Refactoring the tests Adding operations Learnings from our first TDD code Assertions Advanced testing with mocks, spy, and others Testing in Isolation Dummy test double: when we don't need to test the collaborator Fake test double: a simplified collaborator Stub test double: a predefined collaborator Spy test double: verifying collaboration Mock test double: asserting collaboration UI testing with Xcode The importance of UI testing Recording a UI test Writing UI tests in code Tips and tricks Testing singletons Testing Async code Run only the test with the cursor Summary Going Out in the Open (Source) Documenting Swift The Markdown language The anatomy of a documentation block Rich content Additional callouts Structural annotations Generating HTML docs Publishing to GitHub Pages Continuous integration Travis CI Configuring simple projects Configuring more complex build scenarios Configuring pure Swift projects GitLab.com Building and testing Adding a linter, SwiftLint Some final words on Travis and GitLab Using fastlane for automated delivery Getting started with fastlane Your first lane Fastlane beta Using Travis to upload on tags Becoming a maintainer, tips and tricks The README.md file The LICENSE.md file The CODE_OF_CONDUCT.md file Issues, Pull Requests, and more No is temporary, yes is forever Summary Other Books You May Enjoy Leave a review - let other readers know what you think