1 00:00:00,480 --> 00:00:06,160 Sober come back again in this class and we're going to talk about a feature of programming for Iowas 2 00:00:06,180 --> 00:00:12,270 which is called category's and answer specific to Objective C categories are a way for us to add the 3 00:00:12,270 --> 00:00:17,990 functionalities and add new methods to existing classes. 4 00:00:18,000 --> 00:00:23,610 Let's see how we could do that before I actually go to school them you know try to make an app that 5 00:00:23,610 --> 00:00:25,080 explains category's. 6 00:00:25,110 --> 00:00:29,680 I want to show you that you somehow have been seeing categories all along the way. 7 00:00:29,730 --> 00:00:30,690 I'm going to take a nap. 8 00:00:30,690 --> 00:00:32,030 There's going to be a temporary hour. 9 00:00:32,040 --> 00:00:36,850 You don't have to make this it's going to be a single view application in Objective C and I'm going 10 00:00:36,860 --> 00:00:40,370 to call it tamp up. 11 00:00:40,570 --> 00:00:45,250 And if you remember whenever we make one of these applications you know objective Objective-C immediately 12 00:00:45,250 --> 00:00:52,750 end up seeing something like this at interface view controller a known name in ASH and something in 13 00:00:52,750 --> 00:00:56,590 the past I probably told you this is called a private category. 14 00:00:56,820 --> 00:00:58,030 And that's exactly what it is. 15 00:00:58,030 --> 00:01:05,080 It's a private category that helps us put private stuff in here things that we don't want our data h 16 00:01:05,080 --> 00:01:10,730 to have it's interface to have it so it cannot be exposed to other classes. 17 00:01:10,750 --> 00:01:17,950 As you can imagine any class that imports this one view controller will have access to whatever is here 18 00:01:18,220 --> 00:01:19,700 which is in our data h. 19 00:01:19,750 --> 00:01:25,810 So we use this one as some sort of a private place for declaring different stuff that we don't want 20 00:01:25,810 --> 00:01:31,800 anyone else to essentially having access to them or we don't want them to be exposed outside. 21 00:01:31,810 --> 00:01:36,570 So that's kind of how you declare and use a category. 22 00:01:36,580 --> 00:01:40,260 Now let's go ahead and see how we will do it in a much simpler way. 23 00:01:40,570 --> 00:01:46,930 I'm going to go to school it's say fine new project is going to be a command line tool in my class and 24 00:01:46,930 --> 00:01:48,340 I'm going to call this one. 25 00:01:48,340 --> 00:01:54,390 Categories are total categories tool. 26 00:01:54,400 --> 00:02:01,500 The only thing it has to be objective c of course when added in here is what they want to do. 27 00:02:01,540 --> 00:02:04,610 I want to go to my dad and my main fine. 28 00:02:04,780 --> 00:02:06,870 I want to have a string. 29 00:02:07,120 --> 00:02:09,150 Let's say that is this thing. 30 00:02:09,160 --> 00:02:10,230 Hello world. 31 00:02:10,270 --> 00:02:11,050 All right. 32 00:02:11,050 --> 00:02:13,000 It is a. 33 00:02:13,290 --> 00:02:17,470 It is called the hello as we are and that is this one. 34 00:02:17,780 --> 00:02:20,060 I want to have a way to reverse it. 35 00:02:20,140 --> 00:02:28,210 Essentially say r t s or l e h but I don't want to have a function here and I don't want to have it 36 00:02:28,420 --> 00:02:35,740 as a separate class simply because I want the string to be able to do that very much like the way that 37 00:02:35,770 --> 00:02:37,620 you could say hello. 38 00:02:37,680 --> 00:02:43,240 Or a string by appending a string which is a function built into a string. 39 00:02:43,370 --> 00:02:48,230 I want to make it like that simply because this is a function that I'm going to be using on and on. 40 00:02:48,310 --> 00:02:53,590 Of course reversing it is not necessarily that case but you understand the meaning of it you could do 41 00:02:53,590 --> 00:02:57,570 something that you can always use from that particular class. 42 00:02:57,580 --> 00:02:59,730 So here's how we're going to do that. 43 00:02:59,760 --> 00:03:06,250 I'm going to go up here say it's interface just like all of you have always been doing are declaring 44 00:03:06,250 --> 00:03:14,060 classes but this interface is actually going to be a string type and it doesn't have a superclass. 45 00:03:14,080 --> 00:03:17,660 It doesn't have a call of course but it does have two pounds. 46 00:03:17,670 --> 00:03:21,340 Is that a stand for the name of the category. 47 00:03:21,340 --> 00:03:25,350 And I'm going to call this category reversal. 48 00:03:25,680 --> 00:03:29,100 It's going to be my reverse third category of any system. 49 00:03:29,110 --> 00:03:32,890 And of course I have implemented its implementation. 50 00:03:32,890 --> 00:03:34,150 What do you want to implement. 51 00:03:34,150 --> 00:03:39,810 I want to implement this guy and then it does whatever it does and then we'll have to deal with that. 52 00:03:39,940 --> 00:03:48,490 So the first thing I should do I should say I have a method in here that returns a string that's called 53 00:03:48,520 --> 00:03:49,760 reverse. 54 00:03:50,800 --> 00:03:55,210 So this method whenever we send something to it it has to reverse it. 55 00:03:55,210 --> 00:03:59,400 So I'm going to say this thing is within the implementation as well. 56 00:03:59,590 --> 00:04:06,490 And it has an end as a string let's call it the raise and return address. 57 00:04:06,760 --> 00:04:11,410 Now obviously between these two we have to actually reverse it. 58 00:04:11,410 --> 00:04:13,390 I'm not talking about that real soon. 59 00:04:13,390 --> 00:04:19,370 But before I do that I want to put my interface and my implementation in separate files. 60 00:04:19,510 --> 00:04:27,040 So I'm going to go ahead and say file mewl file my new file is going to be a cocotte class of that type 61 00:04:27,160 --> 00:04:30,720 and it's a string because that's what we're trying to make. 62 00:04:30,790 --> 00:04:34,480 And there's a very particular way for naming categories. 63 00:04:34,480 --> 00:04:40,240 You don't have to be it's a very very common practice and that is called and that's a string and then 64 00:04:40,240 --> 00:04:47,320 plus whatever it does for instance in this case it becomes an sustaining Plus the reverse. 65 00:04:47,550 --> 00:04:50,510 This is minus things category that reverses. 66 00:04:50,540 --> 00:04:56,320 And I think so if you find the file outside that somebody else has written and it has this particular 67 00:04:56,320 --> 00:05:00,360 format it has a plus by then and then it has some sort of label. 68 00:05:00,400 --> 00:05:02,800 It means that it does that thing. 69 00:05:03,010 --> 00:05:08,540 So I'm going to say next I have it in here over there the house. 70 00:05:08,740 --> 00:05:10,790 And then I'm going to copy this to. 71 00:05:10,840 --> 00:05:13,000 So that's quite unnecessary. 72 00:05:13,300 --> 00:05:18,060 Reverse the implicit interface of it somewhere pasted in here. 73 00:05:18,820 --> 00:05:25,190 And then again I'm going to call this one implementation Olduvai. 74 00:05:25,390 --> 00:05:28,960 I'm going to put my that in fact. 75 00:05:29,110 --> 00:05:30,340 So that's about that. 76 00:05:30,340 --> 00:05:33,100 This is my dad H and this is my dad. 77 00:05:33,220 --> 00:05:38,610 And of course I can and my dad use it but we don't really know how it is going to work just yet. 78 00:05:38,620 --> 00:05:43,900 So the next thing is write the body of here to do reverse smy a string. 79 00:05:44,100 --> 00:05:45,840 So let's see how we going to do that. 80 00:05:45,840 --> 00:05:47,610 Here is the logic behind it. 81 00:05:47,610 --> 00:05:53,370 Let's say I have this very long set of characters I'm going to say first of all find out how many are 82 00:05:53,370 --> 00:05:53,630 there. 83 00:05:53,670 --> 00:06:00,780 I can count this but let's say 25 and then we're going to say well if they're 25 get this started from 84 00:06:00,780 --> 00:06:03,190 24 which is that one. 85 00:06:03,630 --> 00:06:10,710 Then add the 2010 which is that one and then add the 20th second which is that one so on and so forth 86 00:06:11,010 --> 00:06:13,790 until you reach to the very beginning of it. 87 00:06:13,950 --> 00:06:19,400 And that would be essentially the technique I'm going to be using to reverse a string. 88 00:06:19,590 --> 00:06:24,310 So let's go ahead and implement that since I'm going to be changing this text. 89 00:06:24,330 --> 00:06:25,750 I have to change this into. 90 00:06:25,800 --> 00:06:27,800 And this mutable string. 91 00:06:27,940 --> 00:06:30,030 It has to be an immutable string. 92 00:06:30,120 --> 00:06:38,280 And then I'm going to go ahead and say without initialises of course and US mutable string new then 93 00:06:38,340 --> 00:06:42,270 I have to find out the how how long is my time. 94 00:06:42,360 --> 00:06:47,790 I'm going to go ahead and say it is an integer and that integer is called. 95 00:06:47,820 --> 00:07:01,470 Let's say there are lengths for instance or a string length string length is going to be self simply 96 00:07:01,470 --> 00:07:06,180 because V or of it in the end it's a string as a cell. 97 00:07:06,360 --> 00:07:08,570 So how long is it. 98 00:07:08,670 --> 00:07:12,810 This is for instance the one that returns 25 for us. 99 00:07:12,810 --> 00:07:15,930 Now I could simply put this inside a 42. 100 00:07:15,990 --> 00:07:17,160 I could go ahead and say. 101 00:07:17,160 --> 00:07:21,460 Or better yet unaffordable for a situation like this is a while. 102 00:07:21,540 --> 00:07:30,820 So I'm going to go ahead and say while the condition is this while this guy is still larger than zero. 103 00:07:30,840 --> 00:07:31,260 All right. 104 00:07:31,260 --> 00:07:37,950 So as we go through it Dad dad dad dad we go there as long as we're here at least we are zero. 105 00:07:37,950 --> 00:07:39,100 You'd do this. 106 00:07:39,180 --> 00:07:42,600 You say first of all subtract from yourself. 107 00:07:42,600 --> 00:07:46,040 So it becomes a one less imagine indice 25. 108 00:07:46,080 --> 00:07:47,660 We are Cranley landing. 109 00:07:47,700 --> 00:07:49,710 I don't know somebody like 17. 110 00:07:49,830 --> 00:07:51,840 So we are here on this. 111 00:07:51,960 --> 00:07:59,820 So we have to go and say here right before W go for one candidate and that we do it with something called 112 00:07:59,880 --> 00:08:01,050 arr.. 113 00:08:01,050 --> 00:08:04,270 So here is how we introduce a range and then I say. 114 00:08:04,340 --> 00:08:09,580 A syringe and in my honest range I'm going to call it substring range. 115 00:08:09,570 --> 00:08:16,230 That's a very good name usually substring range is and this may range and the range we want to make 116 00:08:16,230 --> 00:08:19,470 is from here for one CACs. 117 00:08:19,680 --> 00:08:24,890 So as you can imagine it says Major range from 17 12:56. 118 00:08:24,930 --> 00:08:26,740 So only this one. 119 00:08:26,760 --> 00:08:27,980 So what you want to do with that. 120 00:08:28,050 --> 00:08:29,280 I want to find that. 121 00:08:29,370 --> 00:08:34,890 There's something to say and it's a string of let's call it next block that actually makes more sense 122 00:08:34,890 --> 00:08:36,990 to quite the previous character. 123 00:08:36,990 --> 00:08:46,360 So previous character is going to be self wherever I am substring by range and this range. 124 00:08:46,500 --> 00:08:51,990 So I'm going to say here if we wanted to find 17 and only 17. 125 00:08:51,990 --> 00:08:53,150 Just one of them. 126 00:08:53,160 --> 00:08:57,450 So now go to self find that place. 127 00:08:57,480 --> 00:09:01,300 I'm putting something called previous sky that I'm going to say. 128 00:09:01,340 --> 00:09:05,700 RAZ This one resolved append a string. 129 00:09:05,730 --> 00:09:07,130 What do you want to append to it. 130 00:09:07,220 --> 00:09:11,790 This one I went up with that one and that's about nuts. 131 00:09:11,790 --> 00:09:15,600 Now you manage to go through every one in here and get it. 132 00:09:15,650 --> 00:09:18,050 And at the end return that's value. 133 00:09:18,270 --> 00:09:22,400 So let make clear all of these and just quickly talk about once again. 134 00:09:22,410 --> 00:09:27,840 So what's happening in here is that we have a set of characters we're going to find out how many they 135 00:09:27,840 --> 00:09:28,210 are. 136 00:09:28,210 --> 00:09:34,760 Once we found out how many they are we're going to go from the top and the using of white and up we're 137 00:09:34,810 --> 00:09:40,870 going to stop trying to 1 at the very beginning simply because remember if testing thinking is 25. 138 00:09:40,920 --> 00:09:43,500 Their starting point of them is 24. 139 00:09:43,500 --> 00:09:51,180 So we first subtract one from them and then we find the range based on that number and of one more character 140 00:09:51,720 --> 00:09:53,950 we find that particular character. 141 00:09:53,960 --> 00:09:58,110 We added two hours just so that things are going to become a little bit easier. 142 00:09:58,110 --> 00:10:00,490 I'm going to go ahead and print this value. 143 00:10:00,540 --> 00:10:01,860 You're going to have to do this. 144 00:10:01,910 --> 00:10:03,610 I'm going to say print. 145 00:10:03,630 --> 00:10:07,450 What do you have at every given time for us. 146 00:10:07,500 --> 00:10:11,490 So as we go through this process print it for me. 147 00:10:11,490 --> 00:10:16,960 Now I have managed to add in your category to a string called to reverse it. 148 00:10:17,010 --> 00:10:29,160 If I go to Maine and say import import then is a in reverse it I can go in here and say well and it's 149 00:10:29,180 --> 00:10:29,990 a string. 150 00:10:30,210 --> 00:10:33,660 Reverse the string is. 151 00:10:34,020 --> 00:10:43,700 Hello Esti are reversed it's now reverse it is actually working just like any function from the strings 152 00:10:43,740 --> 00:10:49,720 because it has been added to the string at the implementation level of the string. 153 00:10:49,770 --> 00:10:51,840 There is now a new function. 154 00:10:51,840 --> 00:10:58,260 And now I can go in here and say well show me what is reverse it's so I'm going to say and Aslaug slog 155 00:10:58,950 --> 00:11:01,190 reverse these. 156 00:11:01,460 --> 00:11:04,460 And what is it going to be is going to be this. 157 00:11:04,500 --> 00:11:09,840 And since I know this guy is printing some random stuff I'm actually going to push this down a few lines 158 00:11:10,160 --> 00:11:11,930 so I'm going to sing a few lines down. 159 00:11:12,000 --> 00:11:13,180 So let's run it. 160 00:11:15,530 --> 00:11:18,190 Now it should do what you expected it to do. 161 00:11:18,190 --> 00:11:23,340 Hopefully should say OK. 162 00:11:23,490 --> 00:11:30,720 So first of all it says reverse it is the reverse of not the exclamation mark the L or O.W. so on and 163 00:11:30,720 --> 00:11:31,410 so forth. 164 00:11:31,620 --> 00:11:33,510 And this is the process that went through. 165 00:11:33,510 --> 00:11:39,060 It said The first one then the next one then the next one then the next one all the way to the end. 166 00:11:39,060 --> 00:11:42,830 It found all of them so that worked the way we expected it. 167 00:11:42,840 --> 00:11:49,930 This is how you add categories to objective c which is essentially a way of marginalizing or code. 168 00:11:49,950 --> 00:11:58,050 If that class belongs to loss or a way of adding new functions to our existing classes if we can not 169 00:11:58,050 --> 00:11:59,040 change them. 170 00:11:59,040 --> 00:12:03,610 So and as a string and as array I don't know a text field. 171 00:12:03,690 --> 00:12:10,460 They're all kind of machine classes and if you want to add functionality to them we add them using categories. 172 00:12:10,500 --> 00:12:13,640 So let's keep it up to here and we'll move on to our next lesson.