1 00:00:00,770 --> 00:00:06,250 Indices I'm going to show you how to make classes in Objective C in an ideal situation I would leave 2 00:00:06,250 --> 00:00:09,360 this up to you as an assignment or an exercise. 3 00:00:09,460 --> 00:00:14,680 But I think it helps if I give you some instructions at the beginning about classes in Objective-C. 4 00:00:14,700 --> 00:00:18,570 In theory they do exactly the same thing as the ones in swift. 5 00:00:18,580 --> 00:00:22,490 In practice they are a little bit different in how we called them. 6 00:00:22,630 --> 00:00:27,720 So let's go ahead to X go at the next one I'm going to start a new project command line. 7 00:00:27,760 --> 00:00:34,260 At this time is going to be an objective see one and I'm going to say this is called classes in object. 8 00:00:34,270 --> 00:00:41,950 See so the one big difference between when we do things in Objective science free is if you remember 9 00:00:41,950 --> 00:00:46,240 in objective C-v always had two files we had the dot h n m. 10 00:00:46,420 --> 00:00:52,720 And that's kind of for a reason that's there because they are to declare classes in two separate places. 11 00:00:52,870 --> 00:00:59,370 So to declare a class in Objective-C I'm going to say at interface and then my class name I'm going 12 00:00:59,370 --> 00:01:03,370 to call this one class to be a car for instance. 13 00:01:04,030 --> 00:01:10,990 And then unlike Strief classes in Objective-C absolutely need to have a super class or what we call 14 00:01:11,020 --> 00:01:12,530 a base class. 15 00:01:12,580 --> 00:01:16,600 Now a base class is essentially something that we inherit from. 16 00:01:16,600 --> 00:01:23,140 For instance you might be writing something that is a new feature to an array and then this would be 17 00:01:23,140 --> 00:01:24,400 common in this array. 18 00:01:24,400 --> 00:01:27,400 I'm trying to make a subclass of an array. 19 00:01:27,490 --> 00:01:33,170 If you don't know what subclass you have to use it's not exactly scientific. 20 00:01:33,180 --> 00:01:37,210 But as a rule of thumb you can say you don't know what's the superclass. 21 00:01:37,210 --> 00:01:42,030 I can stick to a subject but most likely you wouldn't know what to use. 22 00:01:42,130 --> 00:01:44,640 For instance when we make an appt. 23 00:01:44,650 --> 00:01:45,750 You don't need to do this part. 24 00:01:45,760 --> 00:01:47,230 I'm just going to show it to you. 25 00:01:47,270 --> 00:01:52,350 Then we make a template up. 26 00:01:52,530 --> 00:02:00,690 If you remember our interfaces in our view controller are subclasses of you eye view controller simply 27 00:02:00,690 --> 00:02:05,300 because my view contouring needs to inherit things from your view controller. 28 00:02:05,310 --> 00:02:10,100 We'll talk about this more when we talk about inheritance there's going to be a lesson about that. 29 00:02:10,350 --> 00:02:17,040 But for now if you're making an interface and you don't know the superclass of it you can live with 30 00:02:17,160 --> 00:02:18,070 a subject. 31 00:02:18,270 --> 00:02:22,650 Once you have a class interface or you have your class figuration. 32 00:02:22,650 --> 00:02:25,650 I'm going to have to go ahead and add an implementation. 33 00:02:25,650 --> 00:02:31,480 And that's essentially saying what are the different a specification of my card. 34 00:02:31,740 --> 00:02:34,610 This is exactly like the one that I'm going to reopen this. 35 00:02:34,630 --> 00:02:40,200 Again is exactly like the one that we have in view controller data and that's the implementation of 36 00:02:40,200 --> 00:02:41,320 our view controller. 37 00:02:41,460 --> 00:02:47,070 If you remember I told you we can get rid of this private interface and then we have the implementation 38 00:02:47,070 --> 00:02:50,380 view controller and we have the interface View Controller. 39 00:02:50,520 --> 00:02:52,430 So in here we have the same thing. 40 00:02:52,440 --> 00:02:56,850 Once we have an interface of car we have the implementation of the car. 41 00:02:56,850 --> 00:02:59,170 He's asking me to write the methods for it. 42 00:02:59,310 --> 00:03:05,290 So let's go ahead and add a few properties or attributes to our core and then some methods. 43 00:03:05,400 --> 00:03:12,180 The first property that I want to add and we are like this property and this thing is going to be called 44 00:03:12,180 --> 00:03:17,550 a name that is the name of the car object we're about to make. 45 00:03:17,670 --> 00:03:26,650 If this was a real car in let's say a game that you're writing it would have model and color is speed 46 00:03:26,660 --> 00:03:33,450 a number of years so on and so forth for now and then have only one more and that is property that's 47 00:03:33,450 --> 00:03:39,720 going to be a boolean property is called ease on which essentially defines whether the car is currently 48 00:03:39,720 --> 00:03:40,840 on or not. 49 00:03:40,860 --> 00:03:44,690 I'm also going to add two functions or two methods to our cars. 50 00:03:44,880 --> 00:03:47,620 I'm going to say one of them is art. 51 00:03:47,830 --> 00:03:50,620 The engine on. 52 00:03:50,640 --> 00:03:57,420 So that's one method that's going to turn the engine on and they want to have an in it I want it initialises 53 00:03:58,010 --> 00:04:01,610 I want any show they want to override my initializers. 54 00:04:01,800 --> 00:04:07,120 So this one is a little bit different than what we had in in swift so in here. 55 00:04:07,530 --> 00:04:13,890 As always when we are writing something we have to actually tell it what kind of return type it has 56 00:04:14,310 --> 00:04:20,190 and the return type of it will be something that we call instance type means of whatever is the type 57 00:04:20,190 --> 00:04:27,210 of this car return that same it means when you are writing this method if it is going to be or initialiser 58 00:04:27,570 --> 00:04:30,170 return it of the same type. 59 00:04:30,180 --> 00:04:36,620 The next thing which is very important it has to begin with in it its name has to have in it. 60 00:04:36,840 --> 00:04:42,740 And then you can add whatever you like and will say in it with the state on what's the estate. 61 00:04:42,750 --> 00:04:50,220 The estate is a Boolean OR they call it our current or is beginning a start a state. 62 00:04:50,490 --> 00:04:57,120 So that's essentially initializing a car with a beginning or a starting state. 63 00:04:57,120 --> 00:05:03,000 Imagine you have your car on at the very beginning or you want the engine to be off at the very very 64 00:05:03,000 --> 00:05:04,060 beginning. 65 00:05:04,140 --> 00:05:07,640 Now so far and I think that this may drop that. 66 00:05:07,770 --> 00:05:14,730 So far we have managed to write an interface for a car a class declaration for car two attributes and 67 00:05:14,730 --> 00:05:18,510 two functions one of the two functions is in its function. 68 00:05:18,510 --> 00:05:21,610 The other one is just a function that does something. 69 00:05:21,810 --> 00:05:28,200 So now Isaac squaddies right reminding us is saying that the definition for these two methods are not 70 00:05:28,200 --> 00:05:28,520 there. 71 00:05:28,530 --> 00:05:31,120 His things are what you want to do with these two methods. 72 00:05:31,140 --> 00:05:37,620 So I'm going to say well one of them let me copy both of them actually but easier to copy than to retype 73 00:05:37,620 --> 00:05:38,480 everything. 74 00:05:38,530 --> 00:05:42,550 I'm going to say in both of them I want to do some things. 75 00:05:42,730 --> 00:05:46,170 So one of them turn engine on is actually very easy. 76 00:05:46,170 --> 00:05:52,510 I want to say whenever this function gets called ease on it becomes true. 77 00:05:52,530 --> 00:05:58,430 So if the engine is off it becomes on even if it is on stay still comes on. 78 00:05:58,470 --> 00:06:02,600 Now the other method the initialization needs a little bit of explanation. 79 00:06:02,610 --> 00:06:06,790 First of all it has to return something to the team. 80 00:06:06,810 --> 00:06:13,590 It has to return is essentially going to be of the same type or it is going to be self so I'm going 81 00:06:13,590 --> 00:06:17,320 to say first of all and that's the end return. 82 00:06:17,370 --> 00:06:25,670 But then we have something very simple which is is on has to become the very start that we have. 83 00:06:25,660 --> 00:06:31,230 If you want the engine to start on that would become on if you wanted to be off that would become off 84 00:06:31,290 --> 00:06:33,230 I would become false essentially. 85 00:06:33,360 --> 00:06:41,300 The last thing which is actually here the first thing you have to do we have to say self is super super 86 00:06:41,910 --> 00:06:47,690 in it and that's simply because our class actually has a superclass. 87 00:06:47,820 --> 00:06:55,580 So we have to say in a nutshell when you are initializing first initialize this object now that in a 88 00:06:55,590 --> 00:07:02,820 subject and here is I'm kind of bleeding into the inheritance here we are saying car is an object that 89 00:07:02,820 --> 00:07:05,120 has a name and he's on. 90 00:07:05,130 --> 00:07:12,410 So I'm saying first of all make out on a subject to initialize that once you did that for this instance. 91 00:07:12,600 --> 00:07:17,910 Change is on to something new and then return it to us. 92 00:07:17,910 --> 00:07:18,820 And that's about that. 93 00:07:18,840 --> 00:07:20,620 That's everything we needed to do. 94 00:07:20,650 --> 00:07:25,130 So really quickly keep an eye on this so you know what's going on. 95 00:07:25,140 --> 00:07:31,820 Once you are happy with the small one to initialising this for the first time. 96 00:07:31,870 --> 00:07:41,140 So now I'm going to go to my actual program I'm going to say color on this car is called a jeep and 97 00:07:41,140 --> 00:07:51,220 this is going to be car lock and in it it could either do it like that or you could say car. 98 00:07:51,380 --> 00:07:54,410 Let's call this one a whole lot. 99 00:07:54,610 --> 00:08:01,260 And that's going to be car lock and you need to get a state. 100 00:08:01,270 --> 00:08:02,740 And I'm going to say Drew. 101 00:08:03,230 --> 00:08:04,800 So both of them are valid. 102 00:08:04,830 --> 00:08:07,230 Most of them are the ways that we could use them. 103 00:08:07,420 --> 00:08:12,490 So that's how you declare a class in Objective-C. 104 00:08:12,640 --> 00:08:15,440 That's how you introduce attributes to it. 105 00:08:15,520 --> 00:08:17,260 That's how you do functions. 106 00:08:17,350 --> 00:08:23,320 And this is in particular how you do and emits methods for it. 107 00:08:23,320 --> 00:08:29,590 And then of course in the end it meant that you have to return the self and the self has to be whatever 108 00:08:29,590 --> 00:08:34,980 it is to particular US which in Objective C is mandatory as opposed to Swift. 109 00:08:35,140 --> 00:08:41,590 And then when you're using it this is like everything else that we have seen and as an array my array 110 00:08:41,590 --> 00:08:44,290 is an array of locks. 111 00:08:44,580 --> 00:08:45,570 And it. 112 00:08:45,610 --> 00:08:46,140 Right. 113 00:08:46,240 --> 00:08:48,920 That's exactly the same thing as we have in here. 114 00:08:49,090 --> 00:08:53,780 Except here is called Car overlays and Asare or any other state. 115 00:08:53,800 --> 00:09:00,460 And remember we had in it with objects we could actually add a stuffed with on the fly at the initialization 116 00:09:00,460 --> 00:09:01,770 level. 117 00:09:01,930 --> 00:09:03,600 So that's about that. 118 00:09:03,610 --> 00:09:10,660 That's how we managed to add classes to our Objective-C program. 119 00:09:10,970 --> 00:09:12,610 And that began to move one.