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

Naming Things: A Cautionary Tale

There is an edge case with protocol extensions that may prove to be a great source of frustration if you are not careful. In the previous section, you added description to the requirements of Exercise (by making Exercise inherit from the CustomStringConvertible protocol), added a default implementation for description, and added a specific implementation to TreadmillWorkout that took precedence over the default implementation. This worked correctly because description was required by the Exercise protocol. But what happens if you write a protocol extension to add a property or method, then add a property or method with the same name (but a different implementation) to a conforming type?

The answer is ...

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