1 00:00:00,190 --> 00:00:05,670 So well coming again in this listen we're going to talk about selectors and class types. 2 00:00:05,880 --> 00:00:12,390 So select essentially a way for us to find out if a certain object can respond to a certain function 3 00:00:12,810 --> 00:00:19,200 and class type is essentially used to find out the type of the class that has made this current object 4 00:00:19,470 --> 00:00:25,830 essentially a way to find out which class has instantiated this particular object. 5 00:00:25,830 --> 00:00:27,830 Now let's go to school and get this done. 6 00:00:27,840 --> 00:00:32,260 And just so that you know every once in a while we go back to the whole objective see mode. 7 00:00:32,400 --> 00:00:35,650 I'm going to do both of these in Objective-C this time. 8 00:00:35,970 --> 00:00:44,640 So in school I'm going to go ahead and say Give me a new command line tool in Objective-C and it's got 9 00:00:44,640 --> 00:00:47,510 selectors Objective-C 10 00:00:50,350 --> 00:00:51,930 now in my heart. 11 00:00:52,000 --> 00:01:02,240 Emma I'm going to go ahead and say I have something called an s mutable array called my array which 12 00:01:02,240 --> 00:01:03,370 is and is mutable. 13 00:01:03,380 --> 00:01:08,380 Array new is just a brand new allocator initialized array. 14 00:01:08,420 --> 00:01:10,950 Then I'm going to go ahead and do this. 15 00:01:10,970 --> 00:01:21,070 I'm going to say my array perform selector Vitt object and the selector they want to apply to it is 16 00:01:21,080 --> 00:01:29,260 called that selector that is called add object for instance and the object that when out is perhaps 17 00:01:29,270 --> 00:01:29,750 mining. 18 00:01:29,870 --> 00:01:31,820 That's all I want to do in here. 19 00:01:31,940 --> 00:01:35,980 Now you might be asking yourself Is this a safe way of doing it. 20 00:01:35,990 --> 00:01:43,010 Are you just assuming that mutable array no matter what can take an object to it. 21 00:01:43,010 --> 00:01:48,560 And the answer to that is no it actually another very safe way of doing it and much safer way would 22 00:01:48,560 --> 00:02:00,790 be nice if I said if my array responds to selector and this I want to use is called selector add object 23 00:02:01,850 --> 00:02:07,010 if that is true then I want to do this whole thing. 24 00:02:07,180 --> 00:02:08,880 Otherwise I don't mind waiting. 25 00:02:08,990 --> 00:02:17,030 So that's about that if my error rate response to this selector then do that thing add to it for instance 26 00:02:17,090 --> 00:02:22,970 a value and that's pretty much all there is to a selector in the past you have seen selectors that I 27 00:02:22,970 --> 00:02:26,320 as an Baden's For instance I said My. 28 00:02:26,420 --> 00:02:37,090 Then as the target target was self with selector and the selector was at select store on name of functions 29 00:02:37,100 --> 00:02:38,500 so on and so forth. 30 00:02:38,510 --> 00:02:46,090 And that's how we use selectors to make sure that something does respond to a certain function not a 31 00:02:46,100 --> 00:02:48,910 much bigger question for you might be so. 32 00:02:48,980 --> 00:02:53,610 I know this is an array and I know it can have an object. 33 00:02:53,780 --> 00:02:55,810 So what's the point of using this. 34 00:02:55,820 --> 00:03:02,060 So the answer to that would be in a situation like this when we have an array of objects that we don't 35 00:03:02,060 --> 00:03:03,400 know what they are. 36 00:03:03,590 --> 00:03:08,960 So let's go ahead and make that and I'm actually going to make this a slightly more broader application 37 00:03:08,960 --> 00:03:12,270 so I can use it for class type husband. 38 00:03:12,410 --> 00:03:18,030 So I'm glad you don't say command and I'm going to make a cocoa class of the type in its object language 39 00:03:18,030 --> 00:03:21,670 is objective c I'm going to call it car. 40 00:03:22,040 --> 00:03:25,250 So I have a daughter age and a dot M for a car. 41 00:03:25,310 --> 00:03:37,280 And I also have at that age that am for a person and I have a last command an adult age an adult am 42 00:03:37,310 --> 00:03:39,540 for let's say a building. 43 00:03:39,770 --> 00:03:42,200 So cars buildings and people. 44 00:03:42,470 --> 00:03:44,310 And we have three classes in here. 45 00:03:44,560 --> 00:03:47,520 Now I'm going to go do some clean up in all of those. 46 00:03:48,050 --> 00:03:53,540 So you know things are a lot easier for us to see and to understand that that ends I don't have anything 47 00:03:53,570 --> 00:03:54,410 into that. 48 00:03:54,560 --> 00:03:57,350 Again I'm going to remove the nullable again. 49 00:03:57,620 --> 00:04:04,490 And then the same thing in year is going to be rather simple so don't worry about having too many files 50 00:04:04,490 --> 00:04:06,790 and everything is going to be much easier. 51 00:04:07,190 --> 00:04:13,940 So the first thing I want to do is I want to go to the car at that age and say every one of these classes 52 00:04:13,940 --> 00:04:18,360 says they have a method called the law Kate. 53 00:04:18,440 --> 00:04:26,050 So that's essentially going to place my objects my cards my people my whatever in some place. 54 00:04:26,060 --> 00:04:33,270 Also of course they have something called Change gear of not like that. 55 00:04:33,590 --> 00:04:35,940 Not like that change. 56 00:04:36,230 --> 00:04:43,520 So they have two methods and get a copy that I'm going to go to my dad and doesn't have to have an actual 57 00:04:43,520 --> 00:04:44,380 implementation. 58 00:04:44,390 --> 00:04:48,470 As long as we have the curly brackets so that's that. 59 00:04:48,600 --> 00:04:55,160 I'm going to go to my person and say well my person does have it ok but cannot change gear but can't 60 00:04:55,190 --> 00:05:04,070 eat somebody copy these to go to my person that am on say do the same thing have a body for the functions 61 00:05:04,340 --> 00:05:07,040 so you don't get an error when you go to the building. 62 00:05:07,310 --> 00:05:08,970 The building they have it. 63 00:05:09,110 --> 00:05:17,570 And they can not have something that does eat but they can have something called Lights on the lights 64 00:05:17,570 --> 00:05:21,490 they turn on them and they copy those and go to the top. 65 00:05:21,920 --> 00:05:24,980 And of course the same thing as always. 66 00:05:25,550 --> 00:05:32,780 So now I have three classes that the three of them they have a method in them called locate bought each 67 00:05:32,780 --> 00:05:38,180 of them has one extra metadata and yet on the other one lights on. 68 00:05:38,180 --> 00:05:44,090 Now I'm going to go to my dad and I'm going to import all three of them and when I say import or whatever 69 00:05:44,090 --> 00:05:52,190 name port is call and I want to go ahead and say building and I want to go ahead and say person. 70 00:05:52,190 --> 00:05:58,790 So all three of them are either in here and in my array before I do any of these things I'm going to 71 00:05:58,790 --> 00:06:01,030 go ahead and make three copies. 72 00:06:01,130 --> 00:06:08,060 I'm going to say car let's say Jeep is going to be our car. 73 00:06:08,210 --> 00:06:11,180 You know I have one jeep. 74 00:06:11,180 --> 00:06:21,520 My copy is twice even though that was a very good idea person myself is person you're building. 75 00:06:22,640 --> 00:06:28,430 Let's call it of Empire State. 76 00:06:28,460 --> 00:06:30,850 I don't know a name of any buildings to be honest. 77 00:06:30,860 --> 00:06:35,010 All the buildings are numbered like the number 226 or whatever. 78 00:06:35,390 --> 00:06:36,500 So that's about that. 79 00:06:36,510 --> 00:06:37,730 So I have a car. 80 00:06:37,760 --> 00:06:44,840 I have a person and I have a building and they're all added to my array just so that they can make things 81 00:06:44,840 --> 00:06:45,320 easier. 82 00:06:45,320 --> 00:06:56,520 I'm going to cut them up here paste them in here and say I look on to the objects and the objects are. 83 00:06:56,780 --> 00:07:00,530 I mean the Empire State Building. 84 00:07:00,800 --> 00:07:01,260 Perfect. 85 00:07:01,270 --> 00:07:06,810 So now I have an array with mixed objects so I don't know what they are. 86 00:07:06,840 --> 00:07:07,710 I don't know. 87 00:07:07,760 --> 00:07:10,050 You know what do they actually. 88 00:07:10,220 --> 00:07:11,920 What can they do for now. 89 00:07:11,930 --> 00:07:23,080 I could go ahead and say for Gints my ears 0 is a smaller than my array that counts. 90 00:07:23,150 --> 00:07:26,710 And then I press plus do this. 91 00:07:26,750 --> 00:07:27,560 What am I going to do. 92 00:07:27,560 --> 00:07:38,190 I'm going to say if my array object that index responds to selector and let's say the selector that 93 00:07:38,210 --> 00:07:40,310 I want to use it. 94 00:07:40,760 --> 00:07:48,790 If it does respond to it which is this then it's down and eat. 95 00:07:48,830 --> 00:07:55,670 So while this example may not really do anything for us it explains to you why we would end up using 96 00:07:55,670 --> 00:07:56,340 selectors. 97 00:07:56,360 --> 00:08:03,010 We use it for when we don't know what the object is what is it capable of it might be useful. 98 00:08:03,140 --> 00:08:08,000 And you know whether they do respond to a certain select or they don't. 99 00:08:08,000 --> 00:08:13,850 So while we are here let me put on another for loop that is going to tell us what is the class of each 100 00:08:13,850 --> 00:08:15,650 of the guys in my array. 101 00:08:15,830 --> 00:08:20,220 So I'm going to go ahead and say for now here's a very interesting thing. 102 00:08:20,240 --> 00:08:26,180 I don't want to use a numeric for loop I want to use a for loop. 103 00:08:26,270 --> 00:08:34,200 You know usually if you say four and a string for instance Each my array or so on and so forth. 104 00:08:34,290 --> 00:08:37,610 But I know the objects in my array are not unnecessary. 105 00:08:37,620 --> 00:08:39,980 Not only that they could be anything. 106 00:08:40,140 --> 00:08:44,350 So in a situation like that I'm going to say valid for ID. 107 00:08:44,550 --> 00:08:47,510 Each in my array it means go through the array. 108 00:08:47,510 --> 00:08:50,230 Irrelevant of what is inside. 109 00:08:50,230 --> 00:08:53,380 Then I'm going to say for ID each in my array. 110 00:08:53,580 --> 00:08:54,990 Let's do this. 111 00:08:55,170 --> 00:08:59,390 If so what is that for each. 112 00:08:59,410 --> 00:09:01,690 Is kind of class. 113 00:09:01,770 --> 00:09:03,800 And what's the class that you're looking for. 114 00:09:03,810 --> 00:09:08,940 The class is going to be that I'm looking for the ones that are of the type of building I'm going to 115 00:09:08,940 --> 00:09:18,150 say building class if they are of the type building class here you can do whatever you want to do with 116 00:09:18,150 --> 00:09:18,750 the building. 117 00:09:18,750 --> 00:09:25,000 For instance building or turn lights on or anything to that effect. 118 00:09:25,050 --> 00:09:26,470 So that's about that. 119 00:09:26,470 --> 00:09:33,660 So we managed to have it respond to select to find out if these objects respond to a certain thing and 120 00:09:33,660 --> 00:09:37,590 we managed to get the kind of the class that any object is. 121 00:09:37,590 --> 00:09:43,490 And then depending on that you will end up doing things you could know whether it is or whether it is 122 00:09:43,530 --> 00:09:44,070 not. 123 00:09:44,070 --> 00:09:49,290 If you want to filter certain objects out and it's sometimes useful to know what's the kind of the class 124 00:09:49,290 --> 00:09:52,870 that you are dealing the object that you're dealing with. 125 00:09:52,890 --> 00:09:56,340 So that's about that for selectors is kind of class. 126 00:09:56,390 --> 00:09:57,100 Let's move on.