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 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.