Skip to main content
A protocol defines a strict blueprint of methods, properties, initializers, and associated types required to implement a specific piece of functionality. It acts as an interface contract that classes, structures, and enumerations can adopt. When a type satisfies all the requirements of a protocol, it is said to conform to that protocol.

Protocol Declaration and Conformance

Protocols are declared using the protocol keyword. Types declare conformance by appending the protocol name after the type name, separated by a colon. Multiple protocols are separated by commas.

Property Requirements

Protocols can require conforming types to provide specific instance properties or type properties.
  • Properties must always be declared as variables (var), never constants (let).
  • The protocol specifies whether the property must be gettable ({ get }) or both gettable and settable ({ get set }).
  • Type properties are prefixed with the static keyword.

Method Requirements

Protocols can require specific instance methods and type methods.
  • Method signatures are defined without a body (no curly braces).
  • Default parameters are not allowed in protocol method declarations.
  • Type methods are prefixed with static.
  • If a method modifies the instance of a value type (struct or enum), it must be prefixed with the mutating keyword. Classes conforming to the protocol ignore the mutating keyword.

Initializer Requirements

Protocols can require specific initializers. When a class conforms to a protocol with an initializer requirement, the implementation must be marked with the required modifier to ensure all subclasses also provide the initializer. (Structs do not need the required modifier).

Associated Types

Protocols cannot use standard generic type parameters (e.g., <T>). Instead, they use associated types to define placeholders for types used within the protocol’s requirements. The conforming type determines the actual concrete type.

Protocol Inheritance

A protocol can inherit requirements from one or more other protocols. The syntax mirrors class inheritance. Conforming types must satisfy the requirements of the inherited protocols as well as the new protocol.

Class-Only Protocols

To restrict protocol conformance exclusively to reference types (classes), the protocol must inherit from AnyObject. This is commonly used to prevent strong reference cycles by allowing protocol-typed properties to be declared as weak.

Protocol Composition

You can require a type to conform to multiple protocols simultaneously using the & operator. This creates an existential type representing the intersection of the specified protocols. In modern Swift, existential types must be explicitly marked with the any keyword (or some for opaque types).

Protocol Extensions

Protocols can be extended to provide method and property implementations. This allows you to define default behavior for protocol requirements or add new functionality to all conforming types without modifying the conforming types themselves.
Tired of Poor Swift Skills? Fix That With Deep Grasping!Learn More