O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Associated type protocols

So far, have to made functions, methods, and types Generic. Can we make protocols Generic too? The answer is no, we cannot, but protocols support a similar feature named associated types. Associated types give placeholder names or aliases to types that are used as part of the protocol. The actual type to use for an associated type is not specified until the protocol is adopted. Associated types are specified with the associatedtype keyword. Let's examine the following example:

protocol CustomView {     associatedtype ViewType     func configure(view with: ViewType) } 

This protocol defines a configure method that takes any item of the ViewType type. This protocol does not specify how the items in the CustomView should ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required