A Protocol in Swift defines a blueprint of methods, properties, and other requirements that conforming types must implement. Classes, structs, and enums can all conform to a protocol, making protocols a central tool for defining shared interfaces across different kinds of types. A simple protocol might declare a draw() method, and any type that conforms to it must provide an implementation of that method.
Protocols can be enriched with default implementations through protocol extensions. By writing an extension on the protocol itself, developers can provide a body for any requirement, and conforming types will inherit that default behavior automatically. Conforming types can still override the default by providing their own implementation, which makes protocols a powerful tool for adding shared functionality without inheritance.
Protocol composition allows a function or type to require conformance to multiple protocols at once, using the & operator to combine protocol requirements. For example, a parameter declared as Codable & Hashable must conform to both protocols. Extensions in general add new functionality to existing types without modifying their source code, and they can add computed properties, methods, initializers, subscripts, nested types, and protocol conformances. However, extensions cannot add stored properties to a type, since stored properties must be declared in the type's original definition. Extensions are commonly used to organize code by grouping related functionality, often by protocol conformance, which keeps code well-structured without requiring subclassing.