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
folder:
var brian = AngryDog(nickName: "Brian")
var merlin = AngryDog(nickName: "Merlin")
brian.drawSpeechBalloon(message: "Hello, my name is \
(brian.nickName)")
brian.drawSpeechBalloon...