Skip to content

Class-only Protocols

Adding an AnyObject constraint to a protocol can reduce memory use and runtime overhead. Swift only needs to support object references, so it can use smaller storage and simpler operations for copying and releasing protocol values. This can help when storing those values in properties and collections. Protocol method calls still use dynamic dispatch. See Swift’s existential container representation.

When a protocol is in use and only classes or actors conform to it, Periphery suggests adding this constraint.

Disable this analysis with --disable-class-only-protocol-analysis or by setting disable_class_only_protocol_analysis: true in .periphery.yml.