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.2 Classes and Structures

The most powerful way to create new types in Swift, similar to in other object-oriented languages, is by defining classes and structures. At first glance, these look very similar to each other. Let's start with some basic examples to see how to create classes and structures. In the next lesson, we'll go a little deeper into the differences between them.

6.2 Classes and Structures

So we've spent quite a bit of time working with a lot of the built in types when it comes to the Swift programming language. And we've also started to branch over a little bit into this concept of custom types, where we're able to create an enumeration or an enum, which we learned about in the previous lesson. Now we're going to get a little bit further into that world and we're going to start to build our own classes and structures. So what exactly does that mean? Well a class and a structure in the world of Swift are actually very similar concepts with one very important difference, and we'll get to that towards the end of this lesson. But the basic idea here, is that I'm going to create my own type. Just like have an integer or a string or a boolean all built into the Swift programming language, now I can build my own types of very similar to that. So what does that mean for me as a developer, is that I can create these types, and I can model them after real world objects or real world scenarios out there and create that functionality in my application. So let's create a couple samples here. So I'm gonna start by creating a class. And we are going to start by specifying a name. So I can call this anything. I'll just call this my class and as you can see over to the right now of the colon I have the opportunity to specify a super class now. A super class has to do with inheritance and typically object oriented programming and we'll talk about it very briefly, but I'm going to leave most of those topics for another course, so when we will actually start to dig deeper into the world of object-oriented programming in Swift. But, for now, just kind of take it on good faith that it's just a way to create some other sub functionality that this class can inherit and we'll touch on that briefly in a little while. So now I'm going to come in here and as you can see, I can specify any sort of properties and methods that I want. So what are properties? Well, they're pretty much exactly what we've been doing before when we're creating variables and constants. So I can create as many variables, as many constants as I want in here to be able to store information for all for each of the instances that I create of my class. And what's a method? Well a method is actually nothing more than a function that just so happens to live within the definition of a class. And so out in the world you're going to hear a lot of people use functions and methods used interchangeably. And technically speaking, they're not wrong. They're all the same basic concept. Although technically speaking, methods are the name that was kind of given to functions that were specified within a class. But it really shouldn't be something that you worry too much about. You're gonna hear them used a lot interchangeably. So let's start to add in some information here so I can create a basic variable property here. I can have prop one and that could be equal to zero. And that could be my first property and I could create a constant lead prop two, be equal to five, and that's going to be my second one. And I could go on forever and ever doing these types of things. Now in the world of Swift and classes, these types of properties are actually called stored properties. And all a stored property is, is just a simple variable or a constant that are defined within a class that contain a value. That's really all is stored property is. Now, there's another type of property that's actually called a computed property, which doesn't actually store a value, but it actually computes something where you could give it some sort of logic that's ultimately going to return a value or allow you to set a value. And the way that that would work is I could then define it the same way. I could say var and then I'm going to give it a name. And we'll just call this prop three. And then we're going to specify a type here. We're going to say that this is going to be of integer type. And then we're gonna use our open and close parentheses here, so we're gonna define kind of scope that's gonna be within this computed property. And then within here, I can define the types of operations that I'm going to allow this property to do, and there's two different types. There's a Get, and a Set. So I can specify a Get and then within here I could specify the Get functionality that's going to be required to return something, some computed value, hence the name the computed property. And then I could also have a set, which is going to work very similarly, with the exception that for the set, I'm typically going to take in some new value, so I'm going to have an open and close parenthesis here. And within here I'll have some sort of new value. So I can specify a new value when somebody were to create a new instance of my class, and they were to access or assign a value to prop3, then it would execute basically this function right here. And same with the get. If I were to try to get the value of prop3 and do something with it, then this would fire here. And then I could maybe do something some sort of math here. Let's make this a little bit more interesting, we'll say prop one is going to be equal to ten. So I could do some sort of logic in here and ultimately I'm going to have to return something. So we'll just say I'm going to return prop one. So as you can see in my different properties I get access to the other properties so I can say prop one times prop2, like that. And then for set new value, I could maybe take in this value and I could maybe just set prop1 to be equal to whatever this new value is. Something like that. It's kind of a convoluted example, but you'll see the picture here in just a moment. So then in addition to creating these properties, once again remember the stored value and the computed properties. I can also create these methods or these functions that live within my class. So I can define a function. And I'll give it a name just like I would before. I'll call it myFunction. And it'll take in some sort of value and maybe we'll just return an int here. And the return value is simply going to return, say maybe prop2, something like that. But you can have some sort of functionality in there to take care of that for you. So now let's say I wanted to use my class, I wanted to be able to get some values out of it, I need to create a new instance. And so the way that I'm gonna do that is I'll say var, or let, c equal to, or whatever name I wanna give it. Now in a lot of other languages you probably use the new keyword which you're probably familiar with, but in Swift we don't have to do that. We simply specify my class and then we do open and close parentheses. Now I'll show you an example as to how we can pass in some more data here while we use open and close parenthesis in just a second. But now as you can see I could come down and I can investigate c. And c is going to be an instance of my class like we defined up here, and I were to hit dot, I now have access to prop1, prop2, prop3, as well as my function. So I could come in here and I can go ahead and just print out really quickly c dot prop3. So you can see an example of this computed property. I'll go ahead and run my application and down here would hope to see 50. So, as you can see I've accessed my computed property and it multiplied prop1 times prop2. And if I were to come in here and say, maybe, I wanted to change c dot prop three to be equal to, say, 50, then I would assume, at this point, it would actually change prop one here from 10 to 50. So I could go ahead, then, and print c.prop3. And I'll go ahead and run that again and I would assume that this would then make my value even bigger, which it does, I get 250 now. So you can see, I'm starting to create this concept of being able to encapsulate a lot of properties and functionality within a single type that I can then pass around and use to do different operations. Now I know that this is somewhat of a contrived example, but it's going to be very interesting as you move along and get a little bit better and more familiar with some of these concepts. Now, as you saw here I was able to specify I open and close parentheses, and why is that? Well that is the sign that a class or even a structure too allows for something called an initializer. And what an initializer is is just a specialized function or method that lives within my class that's going to allow me to initialize my object and to use some sort of data to start off. So the way that we're going to do that is we can come anywhere within our function here. I usually typically do this at the top, maybe just below some properties, is I can specify an init function or an init method here. Now I can do a couple different versions of this. I can do no parameters, which means I don't have to specify any parameters here, and I'm still going to be able to do something here. So I could say, prop one instead of 10 is going to be equal to one. So if I were to save this, and run my application again, as you saw before, I had 50 and 250, now I get five and 250. So as you can see my initialization function ran as soon as I grabbed a new instance of my class, it set prop1 equal to one instead of 10 and then I got five for my first prop3 access. But I could also start to specify things in here that maybe I wanted to initialize prop1 to be something else when we first started up. So we'll maybe say this is gonna be called p1, and it's gonna be an integer. So then instead of initializing this to be just one, I could initialize prop to be whatever I passed in as p1. So I can save this and now you're gonna see that I'm gonna get an error down here because my class is expecting to get a value in its initializer. So we'll just pass in 20 here is an example. So let's go ahead and try to run that and it's not going to work, because remember with our information on functions we're missing that argument label on p1 so I have to come in here. And I have to specify that p1 because it's my only parameter. So then I can come in here and say that this needs to be 20. So I'll go ahead run that. Now we're are going to get 100 and then 250. So as you can see, I can add some basic initialization by sending data in to a specialized function, called the initialization function, to be able to give some sort of state to my class as I'm getting started. So this is all the basics of a class. So how does this really differ at the end of the day from a structure? Well, it doesn't vary much. So if I come in here and I can actually change class here to struct, and the way that I've actually defined this particular class to this point. Everything is still gonna work, so I go ahead and run this even though it's called MyClass. It's still gonna work the same way, so that's going to beg the question every single time. What exactly is the point? Why am I going to do this? What's the difference between a class and a structure?

Back to the top