O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

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

Protocols

A protocol allows you to define the interface you want a type to satisfy. A type that satisfies a protocol is said to conform to the protocol.

Define a protocol that specifies the interface you need for the printTable(_:withColumnLabels:) function. The function needs to know how many rows and columns there are, what the label for each column is, and what data should be displayed in each cell. It does not matter to the Swift compiler where in your playground file you put this protocol. But it probably makes the most sense to put it just before printTable(_:withColumnLabels:), because you are going to use the protocol in the function.

Listing 19.5 Defining a protocol

...
protocol TabularDataSource {
    var numberOfRows: ...

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