Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.11 Extensions

Extensions are a rather interesting topic when it comes to Swift. Extensions allow you to add functionality to classes and structures that you don’t actually have the source code for. Sounds confusing? I promise that it really isn’t that difficult a concept to master.

Related Links

2.11 Extensions

Over the last couple of lessons we've begun to discuss the concepts of structures and classes, and how to add functionality to them via properties and or methods. Now, let's say this is all fine and dandy, if you are in direct control of the source code for, let's say, this particular calculator class. Then you can add and remove functionality for your, for your use whenever you need to, or whenever the occasion arises that additional functionality is needed or maybe even less functionality. But what happens when you need to add some functionality or it would be nice for you to be able to add functionality to a particular type, or a class, in this case, but you don't have direct access to the source code. Maybe somebody gave you these types and this functionality in a separate library or in a separate file or something along those lines, but you do not have access to change the source code. So you're just provided with this calculator type or this class. So what this lesson is going to introduce to you is the concept of extensions. Now this is not a new concept. In other programming languages, there are the kind, the similar concepts of extensions, or maybe even in, in Objective C, we might be talking about something like a category, and in maybe C-sharp or .NET languages, we could be talking about extension methods. But this even takes that a little bit farther from that realm, in the ability to be able to add properties and methods to our classes even if we don't have the source code. So how does that work? Well, quite simply, actually, we're going to use a keyword known as extension, and then after the extension, we provide the type name, or in our case, the class name. So we're gonna say that this is going to be an extension on the calculator type. Now within here we can start to define methods and properties. But there's one small gotcha that you need to definitely be aware of when you're doing this, is that if you wanted to add a property to the calculator. Let's say we talked in a previous lesson about having like a running total or something along those lines. We simply created a new variable, and here we said var running total, and we said that this was going to explicitly be an integer, and let's initialize this to be 0. What you're going to notice is that you can't actually do this, and if you look at the error, it's gonna tell you that extensions may not contain stored properties, which means these basic properties here, where we're not explicitly doing any getters or setters or what are known as computed properties. Since we're not doing that, we can't actually add this type of a stored property to an extension via, or to a class via an extension. The only thing you can put in here for properties are computed properties. We can do that. We're just gonna have to be a little bit more creative, so in this case, let's say we, for some reason wanted to add some functionality to our calculator where it always returned the double of total. Now, this is a little bit of a contrived example, but you'll get the point. So let's say we wanted to add a property. This is going to be, we'll call it DoubledTotal. And this will be an integer. So it's basically just gonna be double the value that's in the total property of the calculator class, but in this case, we're going to make this a computed property by adding a getter, a setter, or some combination thereof. So in this case, we don't necessarily need a setter. This can be a read-only property, but we're going to provide a getter to this, so that we can get out double of this. Now remember, we can't explicitly state that this is going to be a get. We can absolutely do this, but in this case, since we know it's only gonna be a get, we don't have to get that explicit. We can simply type in the logic that would be contained within that particular getter. And in this case we're going to return Total times 2. So, there we go, there's our extension we've added a property. A computed property to our calculator type. So, now below I can create a new instance of my calculator, just like I've done a few times before. And we'll say that this is going to be a calculator and now I want to maybe add in a number, so we'll just in a 2. And so now we can see that we have a total of 2, but if I were to come down here, I could look into the myCalc instance and I see that we have a doubled total, and this is going to return 4. So very nice. We've been able to add a new property to our method, or to our class, but how can we do that maybe say for a method? Well, we're gonna do it the exact same way. We're going to imagine that we are actually adding this directly to the calculator class. So as you can see we only added in the add functionality up above in our actual definition of the calculator class. So let's go ahead and add in some subtraction. So we'll add in the, a new function, and we'll call this subtract. This is gonna take in an integer, which is going to be an Int. And we're just going to subtract whatever number I pass in here from the total. And because we are in an extension, we get access to, as you saw in the previous example with the property, we have access to all of the properties and methods that are contained within the actual definition of the calculator class. So within here I can now say that total is gonna be equal to minus equal, so that's going to subtract whatever number I provide from the existing total and then assign it to the total property. In this case I'm just going to subtract my I 1, assign that to total. And there we go, so now we have a new method that's accessible on our myCalc. And this is going to be subtract, so now I can say myCalc.subtract and I'm gonna pass in 3. So now as you can see here, we have a total that's equal to negative 1 and I could validate that the double total is still correct and I'm still getting 2 times that negative value. So there as you can see, I've been able to add additional functionality in the form of a calculated property and a method to the calculated class, even though I don't necessarily have access to the source code. Now, yes, I do have this here but if that was in, like I said, in another library or in another file or somewhere else that I can't modify the source, I still have the ability to add some functionality to it if that were to make sense. So, this a very powerful feature of the Swift programming language, that's going to allow us to extend the types even though we may not have access to their source code.

Back to the top