April 2017
Intermediate to advanced
316 pages
9h 33m
English
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 ...
Read now
Unlock full access