liveBook · Manning

Protocol

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A Swift language construct that specifies requirements for types to adopt without defining a class hierarchy.

A protocol specifies requirements that adopting types must satisfy without defining a class hierarchy. Swift uses protocols to put interfaces and composition ahead of inheritance. section 4.1

A graph framework can build on protocols and generics: Graph associates a vertex type with an Edge type, while extensions provide shared operations such as counting vertices, finding indices, and listing neighbors.

A protocol-first city graph

UnweightedGraph<String> stores cities as vertices and uses UnweightedEdge for connections. Adding Seattle–Chicago or Phoenix–Dallas automatically records both directions, and printing the graph shows each city’s neighbors.

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Defines requirements, associated types, extensions, and adoption in a graph framework without developing broader protocol theory.
Examples
Demonstrates Edge, Graph, UnweightedEdge, UnweightedGraph, and a city adjacency-list graph.
What it assumes
Assumes intermediate Swift knowledge while demonstrating generics, Equatable, CustomStringConvertible, and associated types in context.
What you can do after
Build flexible Swift graph types by defining protocol requirements and supplying concrete adopting types.
How it connects
Connects protocols with protocol-oriented programming, composition, inheritance, generics, and graph representations.

Also explained in

Compare how 2 books explain protocol →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.