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

protocol ExerciseType: CustomStringConvertible {
    var name: String { get }
    var caloriesBurned: Double { get }
    var minutes: Double { get }
}
...

Your playground ...

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.