Downcasting with protocols and classes

The ComicCharacter protocol defines one of the method requirements for the drawSpeechBalloon method with destination as an argument of the ComicCharacter type, which is the same type that the protocol defines. The following is the first line in our sample code that called this method:

    brian.drawSpeechBalloon(destination: merlin, message: "How do you do?") 

We called the method defined within the AngryDog class because brian is an instance of AngryDog. We passed an AngryDog instance, merlin, to the destination argument. The method works with the destination argument as an instance that conforms to the ComicCharacter protocol; therefore, whenever we reference the destination variable, we will only be able to ...

Get Swift 3 ObjectOriented Programming - Second Edition now with O’Reilly online learning.

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