Associated types and Self requirements are important features of Swift and are used extensively in the standard library. But they can be tricky to use and continue to frustrate many Swift programmers. By Khawer Khaliq.
The article does a great job explaining:
- What is an associated type
- How not to use protocols with associated types
- A protocol-oriented way to use associated types
- A protocol-oriented alternative to associated types
- What is a Self requirement in a protocol
- A protocol-oriented approach to using Self requirements
… and much more. An associated type is a placeholder in a protocol for a type that is used as part of the protocol. Any type that conforms to the protocol must specify an actual type that will replace the placeholder. A protocol can have one or more associated types and these associated types provide flexibility for conforming types to decide which type to use in place of each associated type placeholder. Good one!
[Read More]