7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 30Length: 4.8 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

6.4 Protocols

Protocols are a more advanced object-oriented programming topic. They are the mechanism that Swift uses to define "contracts" that a type agrees to conform to. In this lesson, I'll show you how protocols are used in Swift.

6.4 Protocols

Another interesting topic that you're going to wind up running across as you're starting to work with different types and creating your own types and classes and those such things is gonna be the concept of a protocol. Now a protocol in other languages, you may have heard them referred to as interfaces and really all they are is a blueprint of sorts in which you can define some methods or properties or other things that can be associated or that need to be agreed to and associated with another type. So, it's really just a way of defining a common set of properties and methods that ultimately a class or some other type agree to conform to. So let's go ahead and see what an example of that might look like. So let's say, we're dealing with some sort of application or some sort of domain where we have to be able to identify individual objects or individual types of some sort within our system and a common characteristic or a common property of those objects maybe is some sort of ID or some sort of identifier. And so what we can do to kind of contain that information is we could create a protocol. And we just do that by, just like we did with a class or a struct or an numeration. We specify the name protocol or the keyword protocol, followed by the name. And so in this case, we'll give it some sort of name that will characterize what we're talking about and we'll call this Identifiable. And what Identifiable is going to do is, it's going to define a series of methods. Or in this case, properties that are going to be used to further define other classes that agree to conform to this protocol. So we would do something like var identifier and then we will specify the type, so maybe it's gonna be an integer will have an integer identifier and then we're also going to specify the accessibility of this particular property. Now it doesn't really matter if this property is going to be a stored property or a computed property, it could be either one and we don't have to specify any of that here. We merely need to specify like I said, its accessibility. So within open and closed curly braces, we can then specify whether or not this is going to be a get property or a set property or both. So in this case, we'll call it a get and a set property. So now we've created this protocol. Now all it has right now in it is a basic property definition here, so how do we use this protocol? So what we would wind up doing is creating another class or some sort of custom type and we'll give this a name. And in this case, we'll just call it something simple like maybe employee. And then over here on the side where it says, super class after the colon. We can specify a super class here or another class that we are going to inherit from, but we can also then specify here, protocols. So, I'm gonna specify here that this is going to agree to implement the Identifiable protocol. So as soon as I do that, you're gonna see here that we're not going to be able to compile this right away, because we get an error here that it says, it does not conform to the protocol Identifiable. And the way that we fix that is by coming in here and implementing this. So we'll say, var identifier and then we'll just give this a type of int. And then of course, this isn't gonna be happy yet, because it doesn't have an initialization method and we could create an initialization method here by passing in some sort of identifier. So we could say, identifier, which is going to be an int. And then within here, we could then specify that self.identifier is going to be equal to identifier. So now that we've done that, we've created a class called Employee that is going to agree to conform to this protocol, which means it needs to have some sort of identifier property. And in this case, that's going to have a getter and setter. And by default, that's what we're going to get here simply by creating this property and then we have an initializer that's going to take in this identifier and then set. Now we don't have to be restricted to just properties when it comes to protocols, we can also define functions or methods that need to be agreed upon to be implemented by a class. So then a simple method that we might be able to do here is maybe we'll have a function and we're only really gonna give a function signature here. So we can say, we need to have a function. That's going to be called validate or something like that and maybe it doesn't take in any arguments and it's going to return a Boolean. So now that we've done that, we're not going to be able to build any more. And you're gonna see that, because Employee does not conform to protocol Identifiable again, because we don't have this function there. So we can come down here and we'll go ahead and implement this function. We'll say, function validate and it recognizes that it needs to have this particular signature. We need to add in some code and all we can do here is add in some sort of logic that's going to then make this function happy. But in this case, we'll simply just return true. So we'll go ahead and save that and we'll build and everything will seem to build correctly. So that's the basic process of creating a protocol that's going to define certain properties or methods or any other sort of characteristics that need to be implemented by a particular class. Now why is this interesting and why is this important? Well, in the world of object oriented programming, which is a very large topic which we're obviously not gonna spend a lot of time going through in this particular course, but the interesting thing about this is that this protocol is a type in and of itself. Now there's no implementation here, but what you can do is throughout your code and throughout your application. You can use that particular protocol as a type and then you are able to actually pass in to say, a function or an initialization method or anything like that an instance of a class or a type that implements that protocol to be able to use it to really add some flexibility to your code so. As an example, let's say here that we have another class. So I'll just copy this will copy this and we'll come down here and we'll paste it and we'll say that an Employee needs to be Identifiable, but maybe so does a Manager. A Manager also needs to be able to do that. And then maybe the validation logic here can be returned self.identifier is maybe >100. And for a Manager, we could use returns self.identifier Something along those lines. And so I can save this. And now if I created a very simple function down here. A function that says, validate person. And then in here, it's going to take an instance of a person and that person is going to be in a class that's going to agree to implement the Identifiable protocol. So now, I can go ahead and return a Boolean in this case. And then down here, I'm simply going to say, I want to return person.validate. Now at this point, we have no process of knowing which version is being passed in. We could pass an instance of Employee, an instance of Manager and it's not really gonna matter either way, because we don't know which one it's going to use. But we don't care, because as long as it agrees to implement Identifiable, we should be fine. So let's very quickly here create two instances, we'll say, var Employee is going to be equal to Employee and we'll go ahead and pass in an identifier and we'll pass in 50. Which as you can see here is not going to validate, because an Employee needs to have an identifier greater than 100 and then will also create a Manager. Which is going to be a Manager, which is going to have an identifier of 50 as well. And so now if I come down here and I make a call to validatePerson, I can pass in an Employee and I can also pass in an instance of a Manager. And it's going to work either way, so I can now save this and run it. And our code is gonna execute two times, which we're not actually gonna be able to see that. So let's go ahead and simply print this. So we'll just print the result of this operation and then we'll print the result of this operation. Save and run. So now, it's gonna run two times. It's gonna print false for the Employee, because we tried to validate it, but the Employee has an identifier of 50, which is not greater than 100. So it doesn't validate, but the Manager has an identifier of 50 and it's less than 100, so it does. So as you can see here, this provides a great opportunity to really create some more extendable code that's ultimately going to allow you to write classes of your own custom type that agree to some sort of protocol that you can pass around throughout your application. And the code doesn't really care what the concrete class is that actually implements the protocol, it only cares that it does implement the protocol. So that's a pretty nice feature to be able to take advantage of.

Back to the top