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 ExerciseType, added a default implementation, and added a specific implementation to Treadmill that took precedence over the default implementation. This worked correctly because description was required by the ExerciseType protocol. What happens, though, if you write a protocol extension to add a property or method, then add the same property or method (with a different implementation) to a conforming type?

The answer is that it depends on how the instance is being accessed – does the compiler know its specific ...

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.