Before start reading this article, I’d highly recommend that you please go ahead and read my following articles.
As usual lets setup our base protocols and classes as below. I hope the code below is self explanatory if you have read my previous article on Protocol.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol WarCapable { | |
var topSpeed : Double {get set} | |
var shotsPerMinute : Int {get set} | |
} | |
protocol SpyCapable { | |
var numberOfCameras : Int {get set} | |
} | |
class WarTank : WarCapable { | |
var topSpeed: Double = 100.0 | |
var shotsPerMinute: Int = 5 | |
} | |
class SpyJeep : WarCapable, SpyCapable{ | |
var topSpeed: Double = 200 | |
var shotsPerMinute: Int = 1 | |
var numberOfCameras: Int = 5 | |
} |
Let’s create some objects now and manipulate the values as below
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//War tank objects | |
var t10 = WarTank() | |
var t11 = WarTank() | |
t11.topSpeed = 110.0 | |
//Spy Jeep Objects | |
var sj10 = SpyJeep() | |
var sj11 = SpyJeep() | |
sj11.topSpeed = 190.0 |
Now it’s time to write some extension for the protocol. This is where things get interesting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extensions | |
extension WarCapable{ | |
func canTravelFaster(item : WarCapable)->Bool{ | |
return self.topSpeed < item.topSpeed | |
} | |
} |
So what actually happens above? Let me explain.
First we have created an extension for the protocol WarCapable. Now inside the extension we have implemented a function. Main difference here in extension is that we are implementing the function but in protocol we will only define the function.
Let’s run some test and check that.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//War tank objects | |
var t10 = WarTank() | |
var t11 = WarTank() | |
t11.topSpeed = 110.0 | |
//Spy Jeep Objects | |
var sj10 = SpyJeep() | |
var sj11 = SpyJeep() | |
sj11.topSpeed = 190.0 | |
// Extensions | |
extension WarCapable{ | |
func canTravelFaster(item : WarCapable)->Bool{ | |
return self.topSpeed > item.topSpeed | |
} | |
} | |
t10.canTravelFaster(item: t11) | |
// returns false | |
sj11.canTravelFaster(item: t11) | |
// returns true |
As you can see above we can compare the top speed of both Tanks and SpyJeeps. But here both Tanks and SpyJeeps are conforming to the WarCapable protocol. What if they don’t? Let’s create another Class and then some objects and check it out.
First create a SpyBike class and confirm it to SpyCapable protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SpyBike : SpyCapable{ | |
var numberOfCameras: Int = 1 | |
} |
Now let’s create some objects for SpyBike
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Spy Bike Objects | |
var sp10 = SpyBike() | |
var sp11 = SpyBike() | |
sp11.numberOfCameras = 2 |
Now if you try to access canTravelFaster method from the WarCapable extension you’ll be thrown with an error. Obviously SpyBike doesn’t conform to WarCapable so it’ll not work
Or other way around will also throw an error as below for the same reason above
As you have guessed, the methods inside the extension will work only with the classes that has confirmed to the mother of that extension. In this case it’s WarCapable.
If I put everything we’ve done so far in one single file. It should look something like below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol WarCapable { | |
var topSpeed : Double {get set} | |
var shotsPerMinute : Int {get set} | |
} | |
protocol SpyCapable { | |
var numberOfCameras : Int {get set} | |
} | |
class WarTank : WarCapable { | |
var topSpeed: Double = 100.0 | |
var shotsPerMinute: Int = 5 | |
} | |
class SpyJeep : WarCapable, SpyCapable{ | |
var topSpeed: Double = 200 | |
var shotsPerMinute: Int = 1 | |
var numberOfCameras: Int = 5 | |
} | |
class SpyBike : SpyCapable{ | |
var numberOfCameras: Int = 1 | |
} | |
//War tank objects | |
var t10 = WarTank() | |
var t11 = WarTank() | |
t11.topSpeed = 110.0 | |
//Spy Jeep Objects | |
var sj10 = SpyJeep() | |
var sj11 = SpyJeep() | |
sj11.topSpeed = 190.0 | |
// Spy Bike Objects | |
var sp10 = SpyBike() | |
var sp11 = SpyBike() | |
sp11.numberOfCameras = 2 | |
// Extensions | |
extension WarCapable{ | |
func canTravelFaster(item : WarCapable)->Bool{ | |
return self.topSpeed > item.topSpeed | |
} | |
} | |
t10.canTravelFaster(item: t11) | |
// returns false | |
sj11.canTravelFaster(item: t11) | |
// returns true |
Hope you’ve learned some more about protocol out of this tutorial. Good luck!
Subscribe to our mailing list
Subscribe to our mailing list and receive a free udpates
Leave a Reply