O'Reilly logo

Object–Oriented Programming with Swift 2 by Gastón C. Hillar

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

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 defined. The following is the first line in our sample code that called this method:

brian.drawSpeechBalloon(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 see what the ...

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