Protocol Inheritance

Swift supports protocol inheritance. A protocol that inherits from another protocol requires conforming types to provide implementations for all the properties and methods required by both itself and the protocol it inherits from. This is different from class inheritance, which defines a close relationship between the superclass and subclass. Protocol inheritance merely adds any requirements from the parent protocol to the child protocol. For example, modify TabularDataSource so that it inherits from the CustomStringConvertible protocol.

Listing 19.12  Making TabularDataSource inherit from CustomStringConvertible

protocol TabularDataSource: CustomStringConvertible { var numberOfRows: Int { get } var numberOfColumns: ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.