Working with methods that receive protocols as arguments

Now, we will create additional instances of the previous classes and call methods that have specified their required arguments with protocol names instead of class names. We will understand what happens under the hood when we use protocols as types for arguments.

In the following code, the first two lines of code create two instances of the AngryDog class named brian and merlin. Then, the code calls the two versions of the drawSpeechBalloon method for brian. The second call to this method passes merlin as the ComicCharacter argument because merlin is an instance of AngryDog, which is a class that implements the ComicCharacter protocol. The code file for the sample is included in the swift_3_oop_chapter_05_10 ...

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.