Default Implementations with Protocol Extensions

Both of the protocol extensions you have written so far add new properties or methods to protocols. You can also use protocol extensions to provide default implementations for the protocol’s own requirements.

Recall from Chapter 19 that the CustomStringConvertible protocol has a single requirement: a readable String property named description. Change ExerciseType to inherit from CustomStringConvertible, meaning it also requires the description property.

Listing 23.9  Making ExerciseType inherit from CustomStringConvertible

p​r​o​t​o​c​o​l​ ​E​x​e​r​c​i​s​e​T​y​p​e​:​ ​C​u​s​t​o​m​S​t​r​i​n​g​C​o​n​v​e​r​t​i​b​l​e​ ​{​ ​ ​ ​ ​v​a​r​ ​n​a​m​e​:​ ​S​t​r​i​n​g​ ​{​ ​g​e​t​ ​}​ ​ ​ ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.