0 1 00:00:00,660 --> 00:00:01,200 Hey, guys. 1 2 00:00:01,230 --> 00:00:04,800 Welcome to another Swift Deep Dive. In this deep dive, 2 3 00:00:04,860 --> 00:00:07,590 We're going to be learning all about Swift Classes, 3 4 00:00:07,650 --> 00:00:11,090 what they are, what they can do, and how to create them. 4 5 00:00:11,190 --> 00:00:18,210 Now, classes like structures are, again, a way of defining a blueprint. They're a way for us to be able to 5 6 00:00:18,210 --> 00:00:24,840 plan our properties and methods. And eventually, when we run our app, be able to initialize our class into 6 7 00:00:24,930 --> 00:00:27,260 an actual object. 7 8 00:00:27,270 --> 00:00:32,910 So in this lesson, I'm going to talk about how to create an object from a class. But in addition, I also 8 9 00:00:32,910 --> 00:00:39,480 want to talk about some of the core concepts of object oriented programming, namely inheritance, subClasses, 9 10 00:00:39,480 --> 00:00:40,630 SuperClasses, 10 11 00:00:40,710 --> 00:00:46,290 what it means to override a method, and what it means to call a method from a SuperClass. 11 12 00:00:47,160 --> 00:00:52,170 So let's create a separate project in Xcode to show these concepts in action. 12 13 00:00:52,170 --> 00:00:57,360 I want you to go to File, New, and choose a New Project. 13 14 00:00:57,390 --> 00:01:03,910 Now, when you see the wizard, I want you to choose macOS and select the Command Line Tool. 14 15 00:01:03,930 --> 00:01:10,980 Now, the reason for this is we're not actually creating an entire app, but we are creating a project which 15 16 00:01:10,980 --> 00:01:18,810 can have multiple swift files associated in the same project. Our project name is going to be called 16 17 00:01:19,050 --> 00:01:26,670 ClassesDemo and then you can go ahead and click Next, and save it anywhere you like. 17 18 00:01:26,880 --> 00:01:29,130 So, this is what you should end up with. 18 19 00:01:29,320 --> 00:01:36,690 And notice how we have a main.swift file that already gets created for us. And inside, all we have 19 20 00:01:36,720 --> 00:01:38,730 is just a print statement. 20 21 00:01:38,730 --> 00:01:44,910 So I want you to delete everything that's currently inside your main.swift. And you'll notice that 21 22 00:01:45,000 --> 00:01:52,290 when we run our project this time, what will happen is it will look inside the main.swift file and 22 23 00:01:52,290 --> 00:01:56,380 run all the lines of code in here as if it were a playground. 23 24 00:01:56,400 --> 00:02:04,450 So if I wrote print "hello," well, then that is what will be run and printed into my debug console 24 25 00:02:04,620 --> 00:02:11,130 when my program runs. So we can expand this and we can collapse the right-hand pane. And in fact, I'm also 25 26 00:02:11,130 --> 00:02:12,960 going to collapse this left-hand pane, 26 27 00:02:12,960 --> 00:02:16,320 so we have a full length debug console. 27 28 00:02:16,320 --> 00:02:21,360 So now it looks pretty similar to what we have when we create a playground. 28 29 00:02:21,810 --> 00:02:26,780 But in this case, I can actually create multiple files inside the same project, 29 30 00:02:26,790 --> 00:02:28,840 my ClassesDemo project. 30 31 00:02:29,250 --> 00:02:36,590 So how do we write the code for our class? Defining a class is very similar to defining a struct. 31 32 00:02:36,720 --> 00:02:43,890 We have the class keyword and then we have the name of the class which is capitalized from the start, 32 33 00:02:44,400 --> 00:02:51,370 and then we have a set of curly braces inside of which we can define the implementation of the class. 33 34 00:02:51,630 --> 00:02:55,380 Let's create a new Swift file for a class in our project. 34 35 00:02:55,380 --> 00:03:01,560 So here I'm going to right-click and I'm going to create a new file in that folder which is going to 35 36 00:03:01,560 --> 00:03:06,490 be a new Swift File, and then hit Next. 36 37 00:03:06,760 --> 00:03:10,820 And now, I'm going to name my file the name of the class 37 38 00:03:10,840 --> 00:03:14,500 that I'm going to create which is going to be called Enemy. 38 39 00:03:14,500 --> 00:03:22,000 So let's imagine that we were building a simple game where we have to create enemy characters for our 39 40 00:03:22,000 --> 00:03:22,500 game. 40 41 00:03:23,170 --> 00:03:26,860 So the first thing I'm gonna do is I'm going to delete all of the code in here. 41 42 00:03:26,860 --> 00:03:33,750 We are going to create our code entirely from scratch. In this enemy.swift file. 42 43 00:03:33,760 --> 00:03:37,780 I'm going to create my Enemy class. To create my class, 43 44 00:03:37,780 --> 00:03:43,300 I'm gonna use the class keyword, and then I provide the name of my class which is going to be called 44 45 00:03:43,450 --> 00:03:51,040 Enemy. And it is convention that when you create a class that the name of the file matches exactly the 45 46 00:03:51,040 --> 00:03:52,690 name of your class. 46 47 00:03:52,690 --> 00:03:57,390 Now, I'm going to open a set of curly braces to define this Enemy class, 47 48 00:03:57,430 --> 00:04:05,320 what it can do, and its properties and its methods. For the Enemy characters in my game, 48 49 00:04:05,320 --> 00:04:11,410 they might have a number of attributes or properties, right? Things like, maybe they would have a health 49 50 00:04:11,410 --> 00:04:20,260 stats, let's say that they start out with 100 health. And they might also have an attack strength, so this 50 51 00:04:20,260 --> 00:04:23,290 is how hard they hit. 51 52 00:04:23,290 --> 00:04:32,980 Now, these are the properties of my Enemy class. But I can also describe behaviors that the enemies can 52 53 00:04:33,220 --> 00:04:34,060 perform. 53 54 00:04:34,060 --> 00:04:37,950 So I would do that by creating a function. 54 55 00:04:38,230 --> 00:04:45,670 Let's say that each of my enemy characters can move, so I could have a move function. And when this move 55 56 00:04:45,670 --> 00:04:54,030 function is triggered, all that happens is that the enemy characters "Walk forwards." 56 57 00:04:54,070 --> 00:05:03,460 Now, we can also define a attack function. And when the enemies attack, what should happen is, maybe they 57 58 00:05:03,460 --> 00:05:11,780 would "Land a hit" and it will do some damage depending on the attack strength of this enemy character. 58 59 00:05:11,830 --> 00:05:17,930 So I'm going to use string interpolation to insert the attack strength value in here. 59 60 00:05:18,260 --> 00:05:25,720 So, for example, a default Enemy character with an attack Strength of 10. When it attacks, it's going to 60 61 00:05:25,720 --> 00:05:35,790 land a hit, and does 10 damage. Now that we've defined our blueprint that defines an Enemy character, it's 61 62 00:05:35,790 --> 00:05:43,470 time to go into our main.Storyboard and we can initialize our Enemy class here and create an object 62 63 00:05:43,560 --> 00:05:47,810 from that class. So to initialize a class, 63 64 00:05:47,810 --> 00:05:53,170 all we do is write the name of the class, and then a set of parentheses. 64 65 00:05:53,660 --> 00:05:57,980 So in our main.swift, I'm going to create a new Enemy. 65 66 00:05:57,980 --> 00:06:06,110 Let's say, it's a little skeleton and I'm going to initialize it as a new Enemy object, 66 67 00:06:06,110 --> 00:06:09,890 so the name of the class and then a set of parentheses. 67 68 00:06:09,950 --> 00:06:15,920 So now that I've got this enemy object which is a skeleton, I can tap into its properties. 68 69 00:06:15,950 --> 00:06:24,080 So, for example, I could print the skeleton.health property. And if I run my app by clicking the 69 70 00:06:24,080 --> 00:06:24,950 play button, 70 71 00:06:27,930 --> 00:06:36,000 you'll see that the skeleton gets created on line two, and on line 3, I'm printing its health which turns 71 72 00:06:36,000 --> 00:06:42,150 out to be 100. And that, of course, comes from the default health property for everything that's created 72 73 00:06:42,150 --> 00:06:45,280 from the Enemy class. 73 74 00:06:45,360 --> 00:06:52,440 Now, in addition to using the properties, I can also get my skeleton to do something, so I can write 74 75 00:06:52,530 --> 00:06:57,210 skeleton.move or skeleton.attack. 75 76 00:06:57,210 --> 00:07:03,360 So now if I run my code, you can see that the health is printed as 100, the skeleton now walks 76 77 00:07:03,360 --> 00:07:13,800 forwards, it lands a hit and does 10 damage. Just by creating this Enemy class, I can now create lots and 77 78 00:07:13,800 --> 00:07:22,530 lots of enemies. I could create more skeletons, so skeleton2 is going to be created again from the Enemy 78 79 00:07:22,530 --> 00:07:31,080 class, and then maybe we could have a skeleton3. and instead of having to create each one of them with 79 80 00:07:31,110 --> 00:07:38,700 a health stat and attack strength, define its behaviors, I've now used that template or that blueprint, 80 81 00:07:38,880 --> 00:07:48,200 the Enemy class, to create three entire skeleton objects which each can move, can attack, have all these properties. 81 82 00:07:48,360 --> 00:07:56,700 So as you can imagine, it's a lot easier to simply replicate and airdrop in all of these different enemies 82 83 00:07:56,970 --> 00:08:04,440 without having to define each of them from scratch. But in the last module, we saw how to use structs 83 84 00:08:04,530 --> 00:08:08,850 or structures. And we could do pretty much the same thing with them, right? 84 85 00:08:09,420 --> 00:08:11,290 But that's not where it ends. 85 86 00:08:11,340 --> 00:08:18,900 The magic of classes really lies within a special capability that they have which is the ability to 86 87 00:08:19,020 --> 00:08:23,410 inherit from a SuperClass. 87 88 00:08:23,500 --> 00:08:26,030 So what do I mean by the SuperClass? 88 89 00:08:26,110 --> 00:08:29,650 And what does it mean to inherit from a SuperClass? 89 90 00:08:29,650 --> 00:08:35,190 Well, the key to understanding this is that we can have a relationship between classes. 90 91 00:08:35,350 --> 00:08:40,900 Think of the SuperClass as a parent. It knows how to do certain things and has certain properties and 91 92 00:08:40,900 --> 00:08:41,860 methods. 92 93 00:08:41,860 --> 00:08:46,950 However, what if we want to take its existing capabilities and build on top of it? 93 94 00:08:46,960 --> 00:08:54,430 Well, yeah, we could do this by adding more code to our existing class. But there's also another way. We 94 95 00:08:54,430 --> 00:08:59,180 can create a SubClass and add our additional functionality there. 95 96 00:08:59,260 --> 00:09:04,690 Think of the SubClass as the child of the SuperClass. The SuperClass is taught the SubClass everything 96 97 00:09:04,690 --> 00:09:06,050 it knows how to do, 97 98 00:09:06,070 --> 00:09:13,000 for example, reading and swimming. And the SubClass can now grow and develop its own capabilities like 98 99 00:09:13,090 --> 00:09:18,530 playing with the iPhone, getting addicted to YouTube, or live streaming on Twitch. 99 100 00:09:18,610 --> 00:09:23,520 In other words, the SubClass has access to all the properties and methods of the SuperClass. 100 101 00:09:23,530 --> 00:09:25,530 That's what we mean by inheritance. 101 102 00:09:25,540 --> 00:09:28,510 Now, let's see inheritance in action in our Xcode project. 102 103 00:09:30,060 --> 00:09:36,360 So let's say that I want to have these generic sort of simple enemies like skeletons. 103 104 00:09:36,360 --> 00:09:41,400 I don't know why skeletons always get a bad rep. They always seem to be the most easy to destroy enemies 104 105 00:09:41,400 --> 00:09:43,210 in any game I've played. 105 106 00:09:43,320 --> 00:09:49,890 But let's say that we wanted to have a more powerful enemy and we wanted to leverage the code that we've 106 107 00:09:49,890 --> 00:09:52,260 created in our Enemy class, 107 108 00:09:52,260 --> 00:09:56,910 but we wanted to make it do more and be more specialized. 108 109 00:09:56,940 --> 00:10:05,730 Let's say that in my ClassesDemo, I create a new file, again, a Swift File, and I name this file Dragon. 109 110 00:10:05,910 --> 00:10:10,190 So I want to create a Dragon class in this file. 110 111 00:10:10,470 --> 00:10:16,890 So I do exactly the same thing as before, but this time I'm going to use the class code snippet that 111 112 00:10:16,920 --> 00:10:20,220 Xcode auto-suggest gives me, and I'm gonna hit enter 112 113 00:10:20,250 --> 00:10:21,220 once I select it. 113 114 00:10:21,810 --> 00:10:29,880 Now, this is the default way that we would create a class, whereas before we created a class that was 114 115 00:10:29,880 --> 00:10:32,940 just a simple standalone class. 115 116 00:10:32,970 --> 00:10:39,690 In this case, I'm going to use the ability of classes to inherit from other classes. 116 117 00:10:39,720 --> 00:10:47,160 Firstly, I'm going to give this class a name which is gonna be my Dragon class and dragons are also a 117 118 00:10:47,220 --> 00:10:47,820 Enemy, 118 119 00:10:47,940 --> 00:10:51,820 so they're going to inherit from the Enemy class. 119 120 00:10:52,260 --> 00:11:00,030 So that means when I create a dragon from the Dragon class, I don't actually have to redefine these properties 120 121 00:11:00,030 --> 00:11:01,500 or these methods. 121 122 00:11:01,500 --> 00:11:07,590 It already has inherited all of those properties methods. And I can show you by simply going into my 122 123 00:11:07,590 --> 00:11:14,220 main.swift and I'm going to delete all of this code here. And this time, I'm going to create a dragon. 123 124 00:11:14,700 --> 00:11:18,210 So the dragon is gonna be created from the Dragon class. 124 125 00:11:18,900 --> 00:11:25,620 And now with my Dragon class being completely empty other than the inheritance from the Enemy class, 125 126 00:11:26,490 --> 00:11:36,010 I'm going to use my dragon object, and I'm going to get it to move and I'm going to get it to attack. 126 127 00:11:36,060 --> 00:11:44,940 So now if I go ahead and delete all of this code and simply go ahead and press run, you can see that 127 128 00:11:45,120 --> 00:11:54,650 when this line 6 runs, the dragon walks forwards, and it also lands a hit when the attack method runs. 128 129 00:11:54,780 --> 00:12:01,350 So without having to lift a finger, essentially, in our Dragon class, we've already managed to inherit 129 130 00:12:01,500 --> 00:12:11,150 all of the capabilities in the Enemy class just by inheriting it like so. Now that's not where it ends though, 130 131 00:12:11,370 --> 00:12:17,010 the whole reason I created this Dragon class is for it to be able to do something different. 131 132 00:12:17,220 --> 00:12:26,600 So let's create a property that is unique to our Dragon class. So maybe it has a wingSpan, and let's set 132 133 00:12:26,600 --> 00:12:29,680 it to default value of 2 meters. 133 134 00:12:29,690 --> 00:12:36,970 So it's a medium-sized dragon. But when I create my dragon object, I could change that. 134 135 00:12:36,980 --> 00:12:42,240 So I could say dragon.wingSpan and let's set it to 5. 135 136 00:12:42,260 --> 00:12:44,370 Let's make it a giant dragon. 136 137 00:12:44,540 --> 00:12:50,680 So this wingSpan property, of course, does not exist in our default sort of Enemy class. 137 138 00:12:50,690 --> 00:12:52,940 I can't say skeleton.wingSpan. 138 139 00:12:53,040 --> 00:12:54,170 That doesn't make any sense. 139 140 00:12:54,170 --> 00:12:56,550 Skeletons don't have wings. 140 141 00:12:57,140 --> 00:13:02,480 So that is a custom property that only exists inside the Dragon class. 141 142 00:13:02,660 --> 00:13:08,660 But, of course, the Dragon class also has access to all the properties that the Enemy class has because 142 143 00:13:08,660 --> 00:13:10,060 it is inheriting from it. 143 144 00:13:10,070 --> 00:13:14,150 So, for example, it has the health property, it has the attackStrength property. 144 145 00:13:14,150 --> 00:13:18,530 So let's set the attackStrength property to a bit higher than your average enemy. 145 146 00:13:18,530 --> 00:13:21,900 Let's make it 15 instead of 10. 146 147 00:13:21,920 --> 00:13:29,060 But in this case, I'm creating a new dragon from the Dragon class and I'm tapping into a property from 147 148 00:13:29,120 --> 00:13:37,000 the SuperClass, so the class that we're inheriting from. Now, I also want my Dragon class to be able to 148 149 00:13:37,000 --> 00:13:41,020 do something that the default Enemy class can't do. 149 150 00:13:41,020 --> 00:13:45,910 Now, it seems to me like from all the movies I've watched, dragons always seem to be able to talk. 150 151 00:13:46,030 --> 00:13:54,550 So let's give our dragon the capability of speech by adding a method code "talk," and this method is 151 152 00:13:54,550 --> 00:14:01,770 going to take a input code "speech", and that's gonna be a string data type, and then all that it's gonna 152 153 00:14:01,810 --> 00:14:09,790 do is just print the words it says whatever the speech happened to be. 153 154 00:14:09,790 --> 00:14:17,430 So I'm just going to insert that speech input in here. And now we've got a dragon that talks. So back 154 155 00:14:17,430 --> 00:14:18,930 in my main.swift. 155 156 00:14:18,930 --> 00:14:30,090 I'm going to say dragon.talk. And the speech that it's going to give is "My teeth are swords! My claws 156 157 00:14:30,150 --> 00:14:34,020 are spears! My wings are a hurricane." 157 158 00:14:34,020 --> 00:14:37,420 So now we've got a dragon that talks, that moves, that attacks. 158 159 00:14:37,530 --> 00:14:46,200 And if I run my code as it is, you can see that my dragon is talking, it's walking for some reason, and 159 160 00:14:46,200 --> 00:14:53,280 it's landing a hit doing a 15-point damage. So you can see we've now got this dragon object created from 160 161 00:14:53,280 --> 00:15:00,420 the Dragon class which can do everything that the enemy can do, but it can do a lot more. 161 162 00:15:00,450 --> 00:15:07,200 So there's some custom things that only the dragon can do that a bog-standard enemy can't. 162 163 00:15:07,320 --> 00:15:14,110 But if you look at our code, when the dragon moves, it probably shouldn't walk. 163 164 00:15:14,490 --> 00:15:17,300 I think it should probably fly instead of walk, 164 165 00:15:17,310 --> 00:15:17,610 right? 165 166 00:15:18,120 --> 00:15:25,620 So in addition to defining custom properties and methods, we can also tap into the methods that are available 166 167 00:15:25,620 --> 00:15:28,200 to us from the SuperClass, 167 168 00:15:28,200 --> 00:15:32,990 so the class that we're inheriting from, and we can override it. 168 169 00:15:33,060 --> 00:15:41,070 So if we tap into the move method, you can see that this override keyword goes in front of our normal 169 170 00:15:41,250 --> 00:15:43,140 function declaration. 170 171 00:15:43,140 --> 00:15:51,090 And this means now that when we trigger the move method, we can say it does something completely different. 171 172 00:15:52,790 --> 00:15:53,510 In my case, 172 173 00:15:53,510 --> 00:16:01,760 I think that when a dragon moves, instead of walking, it probably should "fly forwards." 173 174 00:16:01,760 --> 00:16:08,240 So now if I go back to my main.swift without changing any of this code, I run my code, 174 175 00:16:08,240 --> 00:16:16,220 you can see now when my dragon moves, it flies forwards, instead of walking forwards. And I've managed 175 176 00:16:16,220 --> 00:16:25,430 to achieve that by overriding this method that I got from my SuperClass and providing a custom implementation, 176 177 00:16:25,460 --> 00:16:31,640 so doing something completely different from the move method that the dragon inherited. 177 178 00:16:31,640 --> 00:16:38,280 Now sometimes, however, you actually want the behavior of the method from the SuperClass. 178 179 00:16:38,660 --> 00:16:46,670 So in our case of our dragon, what if I want it to land a hit, do some damage, but also to maybe be 179 180 00:16:46,670 --> 00:16:49,010 able to spit some fire, in addition. 180 181 00:16:49,070 --> 00:16:51,670 Well, in that case, I can do this. 181 182 00:16:52,310 --> 00:17:01,760 I can override the attack method from my SuperClass and I can do something customized to the Dragon 182 183 00:17:01,760 --> 00:17:14,460 class, namely maybe dragons should be able to spits fire and it does 10 damage. But this is not all 183 184 00:17:14,460 --> 00:17:15,550 that I wanted to do, 184 185 00:17:15,630 --> 00:17:23,010 I want it to perform the method that came from the SuperClass, the Enemy class, which I'm inheriting from, 185 186 00:17:23,010 --> 00:17:25,920 but then do some custom stuff as well. 186 187 00:17:25,920 --> 00:17:32,460 So I want both the functionality of the attack method from the SuperClass, as well as some functionality 187 188 00:17:32,670 --> 00:17:35,100 that I've added on as custom. 188 189 00:17:35,130 --> 00:17:44,070 So to achieve this, what I do is I use the super keyword which refers to the Enemy class which I'm inheriting 189 190 00:17:44,070 --> 00:17:44,590 from, 190 191 00:17:44,850 --> 00:17:47,260 and then I write ".attack." 191 192 00:17:47,280 --> 00:17:55,440 So now I'm triggering the attack method from my SuperClass which will print "Land a hit, does X amount 192 193 00:17:55,440 --> 00:18:01,870 of damage," and then I want to "Spits fire and do an extra 10 damage." 193 194 00:18:01,870 --> 00:18:08,940 So now if I go back to my main.swift and I run my code, when dragon.attack gets triggered, my 194 195 00:18:08,940 --> 00:18:11,010 dragon will, first, land a hit, 195 196 00:18:11,010 --> 00:18:19,110 does 15 damage, which comes from super.attack, and then it's going to spit fire and do another 10 damage, 196 197 00:18:19,230 --> 00:18:29,360 which comes from my custom additional implementation of the attack method. So this really is the superpower 197 198 00:18:29,480 --> 00:18:31,290 of classes. 198 199 00:18:31,460 --> 00:18:39,710 You can see that by inheriting from the Enemy class, I saved myself a lot of work and a lot of repetition. 199 200 00:18:39,830 --> 00:18:46,520 When we create functions, we know that they help us reduce the amount of repeated code by packaging a whole 200 201 00:18:46,520 --> 00:18:51,260 bunch of instructions inside the same package. 201 202 00:18:51,260 --> 00:18:57,740 Now, when we create classes and inherit from other classes, we're, again, saving ourselves from creating 202 203 00:18:57,740 --> 00:18:59,450 repeated bits of code, 203 204 00:18:59,450 --> 00:19:04,940 and instead, we are just layering functionality upon a SuperClass. 204 205 00:19:07,630 --> 00:19:14,110 So in fact, without this capability of inheritance that comes from classes, we wouldn't have been able 205 206 00:19:14,110 --> 00:19:17,740 to build any of the apps that we have created so far. 206 207 00:19:17,740 --> 00:19:25,120 If you go to help and develop a documentation, you can see how inheritance is used amongst the components 207 208 00:19:25,120 --> 00:19:31,030 that are part of Apple's UIKit. And that includes almost everything we've shown on screen like 208 209 00:19:31,030 --> 00:19:36,340 UILabels, UISliders, UIImageViews, everything with a UI in front of it, basically. 209 210 00:19:36,640 --> 00:19:41,680 So for example, when we create a button, we're actually creating a UIButton. 210 211 00:19:41,680 --> 00:19:50,020 So if you search for a UIButton and click on it, you can see that this is a UIButton Class and it allows 211 212 00:19:50,020 --> 00:19:58,550 you to respond to user interactions. But this class actually inherits from the UIControl Class, 212 213 00:19:58,930 --> 00:20:05,380 so if we click on UIControl, we get taken to this class' documentation and you can see that this is 213 214 00:20:05,380 --> 00:20:12,310 the base class for all of the controls which convey a specific action or intention in response to user 214 215 00:20:12,310 --> 00:20:13,680 interactions. 215 216 00:20:13,690 --> 00:20:16,530 So we're inheriting some of these capabilities 216 217 00:20:16,540 --> 00:20:26,110 when we use a UIButton. Now, a UIControl itself then inherits from a UIView which is an object that manages 217 218 00:20:26,110 --> 00:20:33,040 the content for a rectangular area on the screen. And then a UIView itself inherits from UIResponder, 218 219 00:20:33,400 --> 00:20:37,780 and UIResponder inherits from NSObject. 219 220 00:20:37,980 --> 00:20:39,900 So what does that mean? 220 221 00:20:39,930 --> 00:20:41,910 Why is it structured like that? 221 222 00:20:41,910 --> 00:20:43,580 Well, let's think about it in reverse. 222 223 00:20:44,100 --> 00:20:47,220 Apple's most basic class is the NSObject. 223 224 00:20:47,220 --> 00:20:49,880 This is the simplest and most generic component 224 225 00:20:50,310 --> 00:20:56,190 and it's how Apple started out. The NS, in fact, stands for NeXTSTEP which was the company that Steve 225 226 00:20:56,190 --> 00:20:59,490 Jobs started when he got kicked out of Apple, initially. 226 227 00:20:59,520 --> 00:21:07,020 Now, the UIResponder can do everything an NS object can do, but it has its own additional capabilities. 227 228 00:21:07,020 --> 00:21:13,380 The UIView in turn builds on top of the capabilities of the UIResponder, and the UIControl builds 228 229 00:21:13,380 --> 00:21:15,870 on top of that again. At each level. 229 230 00:21:15,870 --> 00:21:22,600 more code and more capabilities are added so that components become less generic and more specialized. 230 231 00:21:22,740 --> 00:21:29,400 Until finally, we get a UIButton that the user can press. So now that we've chased it all the way up 231 232 00:21:29,400 --> 00:21:30,090 the tree, 232 233 00:21:30,090 --> 00:21:36,600 we can see how it would take so much longer if we had to create everything from scratch, and create all 233 234 00:21:36,600 --> 00:21:42,300 of our view controllers from scratch, all of our UILabels from scratch, rather than simply just inheriting 234 235 00:21:42,390 --> 00:21:48,720 and using the capabilities that Apple has already created within our UIKit. 235 236 00:21:49,200 --> 00:21:56,700 And this is all possible because of how classes work and how they're able to inherit from other classes. 236 237 00:21:56,820 --> 00:22:02,460 So in the next lesson, we're going to take a look at the difference between struct and classes and when 237 238 00:22:02,460 --> 00:22:03,580 we would use each. 238 239 00:22:03,590 --> 00:22:05,720 So for all of that and more, I'll see you there.