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

Working with methods that receive protocols as arguments

Now, we will create additional instances of the previous classes and call methods that 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.

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:

var brian = AngryDog(nickName: "Brian") var merlin = AngryDog(nickName: "Merlin") ...

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