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(destination: merlin, message: 
    "How do you do?") 
    merlin.drawThoughtBalloon(message: "Who are you? I think.") 

Tip

Bear in mind that when we work with protocols, we use them to specify argument types instead of using class names. Multiple classes might implement a single protocol, so instances of different classes might qualify as an argument of a specific protocol.

The following code creates an instance of the AngryCat class named garfield. Its nickName value is "Garfield". The next line calls the drawSpeechBalloon method for the new instance to introduce Garfield in the comic, and then brian calls the drawSpeechBalloon method and passes garfield as the ComicCharacter argument because garfield is an instance of AngryCat, which is a class that implements the ComicCharacter protocol. Thus, we can also use instances of AngryCat whenever we need a ComicCharacter argument. The code file for the sample is included in the swift_3_oop_chapter_05_10 folder:

    var garfield = AngryCat(nickName: "Garfield", age: 10, fullName: 
    "Mr. Garfield", initialScore: 0, x: 10, y: 20) 
    garfield.drawSpeechBalloon(message: "Hello, my name is 
    (garfield.nickName)") 
    brian.drawSpeechBalloon(destination: garfield, message: "Hello 
    (garfield.nickName)") 

The following code creates an instance of the AngryCatAlien class named misterAlien. Its nickName value is "Alien". The next line checks whether the call to the intersects method with garfield as a parameter returns true. The method requires a ComicCharacter argument, so we can use garfield. The method will return true because the x and y properties of both instances have the same value. The line within the if block calls the moveTo method for misterAlien. Then, the code calls the appear method. The code file for the sample is included in the swift_3_oop_chapter_05_10 folder:

    var misterAlien = AngryCatAlien(nickName: "Alien", age: 120, 
    fullName: "Mr. Alien", initialScore: 0, x: 10, y: 20, 
    numberOfEyes: 3) 
    if (misterAlien.intersects(character: garfield)) { 
      misterAlien.moveTo(x: garfield.x + 20, y: garfield.y + 20) 
    } 
    misterAlien.appear() 

The following code creates an instance of the AngryCatWizard class named gandalf. Its nickName value is "Gandalf". The next lines call the draw method and then the disappear method with misterAlien as the alien argument. The method requires an Alien argument, so we can use misterAlien, which is the previously created instance of AngryCatAlien that implements the Alien protocol. Then, a call to the Appear method for misterAlien makes the alien with three eyes appear again. The code file for the sample is included in the swift_3_oop_chapter_05_10 folder:

    var gandalf = AngryCatWizard(nickName: "Gandalf", age: 75, 
    fullName: "Mr. Gandalf", initialScore: 10000, x: 30, y: 40, 
    spellPower: 100) 
    gandalf.drawAt(x: gandalf.x, y: gandalf.y) 
    gandalf.disappear(alien: misterAlien) 
    misterAlien.appear() 

The following code creates an instance of the AngryCatKnight class named camelot. Its nickName value is "Camelot". The next lines call the draw method and then the unsheathSword method with misterAlien as a parameter. The method requires an Alien argument, so we can use misterAlien, the previously created instance of AngryCatAlien that implements the Alien protocol. The code file for the sample is included in the swift_3_oop_chapter_05_10 folder:

    var camelot = AngryCatKnight(nickName: "Camelot", age: 35, 
    fullName: "Sir Camelot", initialScore: 5000, x: 50, y: 50, 
    swordPower: 100, swordWeight: 30) 
    camelot.drawAt(x: camelot.x, y: camelot.y) 
    camelot.unsheathSword(target: misterAlien)

Finally, the code calls the drawThoughtBalloon and drawSpeechBalloon methods for misterAlien. We can do this because misterAlien is an instance of AngryCatAlien, and this class inherits the conformance to the ComicCharacter protocol from its AngryCat superclass. The call to the drawSpeechBalloon method passes camelot as the ComicCharacter argument because camelot is an instance of AngryCatKnight, which is a class that also inherits the conformance to the ComicCharacter protocol from its AngryCat superclass. Thus, we can also use instances of AngryCatKnight whenever we need a ComicCharacter argument, as follows. The code file for the sample is included in the swift_3_oop_chapter_05_10 folder:

    misterAlien.drawThoughtBalloon(message: 
    "I must be friendly or I'm dead..."); 
    misterAlien.drawSpeechBalloon(destination: camelot, message: 
    "Pleased to meet you, Sir."); 

After you execute the previous lines in the Playground, you will see the following text output:

Brian -> "Hello, my name is Brian"
Brian -> "Merlin, How do you do?"
Merlin -> ***Who are you? I think.***
Garfield -> "Meow Hello, my name is Garfield"
Brian -> "Garfield, Hello Garfield"
Moving AngryCat Mr. Alien to x: 30, y: 40
I'm Mr. Alien and you can see my 3 eyes.
Drawing AngryCat Mr. Gandalf at x: 30, y: 40
Mr. Gandalf uses his 100 spell power to make the alien with 3 eyes disappear.
I'm Mr. Alien and you can see my 3 eyes.
Drawing AngryCat Sir Camelot at x: 50, y: 50
Sir Camelot unsheaths his sword.
Sword power: 100. Sword weight: 30.
The sword targets an alien with 3 eyes.
Alien thinks: I must be friendly or I'm dead...
Camelot === Alien ---> "Pleased to meet you, Sir."

The next screenshot shows the code and the result of executing it in the Playground:

Working with methods that receive protocols as arguments

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.110.194