1 00:00:17,640 --> 00:00:23,640 In this lesson and when I show you a few examples of how to write a class in Strief So that's going 2 00:00:23,640 --> 00:00:29,530 to start with I'm going to go to X court and in a year I'm going to start a new command line tool and 3 00:00:29,830 --> 00:00:32,080 it's going to be a swift language. 4 00:00:32,160 --> 00:00:34,510 I'm going to call the. 5 00:00:34,560 --> 00:00:35,890 Even the strength. 6 00:00:36,000 --> 00:00:40,610 Perfect. 7 00:00:40,620 --> 00:00:48,280 So here's the first example is actually going to be class and I left school autocomplete for me so I 8 00:00:48,280 --> 00:00:49,480 know what they should do. 9 00:00:49,500 --> 00:00:54,020 I have to give it a name I can or can you know not give it a superclass. 10 00:00:54,170 --> 00:00:55,850 And I have to give it some code. 11 00:00:55,870 --> 00:01:01,060 So let's go ahead and write that I'm going to say my first class is a user across. 12 00:01:01,120 --> 00:01:03,160 And what does my User class have. 13 00:01:03,160 --> 00:01:08,910 It has a variable called the name of the Type A string. 14 00:01:09,310 --> 00:01:16,500 Keep in mind that I'm making my name and implicitly forced on route which means name must be there then 15 00:01:16,510 --> 00:01:17,690 I'm gonna save that. 16 00:01:17,830 --> 00:01:20,310 Let's say it have something called for your name. 17 00:01:20,530 --> 00:01:24,370 And that's an option or maybe my user doesn't have a photo. 18 00:01:24,550 --> 00:01:32,440 I have a variable caller ID you ID that my user uses unique ID essentially that's going to be a string 19 00:01:32,440 --> 00:01:34,290 that it must be there. 20 00:01:34,300 --> 00:01:37,740 I cannot not have it and then I'm going to have another thing. 21 00:01:37,750 --> 00:01:44,770 Let's say there are right and this one is going to be an integer and that one should be there as well. 22 00:01:44,770 --> 00:01:45,790 And that's about that. 23 00:01:45,790 --> 00:01:49,550 We now have a class user that has some attributes. 24 00:01:49,750 --> 00:01:52,080 Let's go ahead and build an instance of it. 25 00:01:52,090 --> 00:01:55,830 So this is how to make the class up here. 26 00:01:55,930 --> 00:01:57,200 And this the body of it. 27 00:01:57,330 --> 00:02:00,840 And now we've got to make one instance an instance. 28 00:02:01,000 --> 00:02:09,070 I'm going to say that I'm going to call it my user and new user is of the type the user is going to 29 00:02:09,070 --> 00:02:11,280 be a brand new user. 30 00:02:11,470 --> 00:02:20,280 And then I can say emule user name is going to be staring at me or user dot height. 31 00:02:20,380 --> 00:02:24,210 It has to be an integer value to say 200. 32 00:02:24,520 --> 00:02:27,060 I could not give it a for Tony. 33 00:02:27,070 --> 00:02:28,970 Maybe I don't want to have a photo on him. 34 00:02:29,050 --> 00:02:36,220 I'm going to say I mean use a dollar to you ID is going to be a set of different characters and that's 35 00:02:36,220 --> 00:02:36,700 about that. 36 00:02:36,700 --> 00:02:41,630 Now we have a user that that user is of the type class user. 37 00:02:41,710 --> 00:02:50,050 I could go ahead and say print what I want to print is the user name it is and then I would have one 38 00:02:50,050 --> 00:02:59,300 of these interpretations and I would say you use that name and I have to unwrap it because it's an option. 39 00:02:59,530 --> 00:03:07,780 So let's go ahead and now what I see that's username is exactly the way we expected it to be up. 40 00:03:07,930 --> 00:03:10,510 I should show you something that's completely irrelevant. 41 00:03:10,510 --> 00:03:14,710 But you know it doesn't hurt for producing unique values. 42 00:03:14,710 --> 00:03:26,010 Ian Swift you could use this you ID dot you you I.D. a string and now it will produce a unique value. 43 00:03:26,020 --> 00:03:35,420 So I could say are you ID is and I would get the new ID and you give me a unique ID for this use the 44 00:03:37,480 --> 00:03:44,170 that's a unique number that unique combination that swift produces sometimes when you are you know writing 45 00:03:44,170 --> 00:03:47,620 things internally you would depend on this. 46 00:03:47,710 --> 00:03:54,730 So that was our first example of making a class in my next example I'm actually going to change this 47 00:03:54,730 --> 00:03:55,350 bit. 48 00:03:55,360 --> 00:04:01,900 The one that we do on initialization of our class and you it a custom and it's somebody going here say 49 00:04:02,020 --> 00:04:02,800 in it. 50 00:04:03,040 --> 00:04:08,140 And that's like a default you know class of every default method of every class. 51 00:04:08,140 --> 00:04:11,270 And if you are dead it means you're overriding in it. 52 00:04:11,410 --> 00:04:21,160 So I'm going to say initialize this let's call it input to name a string and string and then input forth 53 00:04:21,200 --> 00:04:22,120 or a name. 54 00:04:22,200 --> 00:04:29,770 That's a string option or maybe there or maybe it is near and then input new ID. 55 00:04:30,370 --> 00:04:31,630 That could be a string. 56 00:04:31,630 --> 00:04:33,170 I'm going to change this in a second. 57 00:04:33,170 --> 00:04:41,020 So if you're typing just hold on for a second and input height and input height it's going to be an 58 00:04:41,020 --> 00:04:42,040 integer. 59 00:04:42,040 --> 00:04:49,180 Now why I say I have to change the input ID because it's most likely that for something like a new ID 60 00:04:49,180 --> 00:04:52,700 you wouldn't know it when you are creating a new user. 61 00:04:52,780 --> 00:04:57,090 If you're not making a new user you probably know their height you know their name if they have a photo 62 00:04:57,100 --> 00:05:01,920 unit you know them and you might want the compiler to produce your ID for you. 63 00:05:01,930 --> 00:05:04,480 So I'm not going to actually input a new idea. 64 00:05:04,720 --> 00:05:12,280 I'm going to say at the end it's time to do this say self that's a name is input to name. 65 00:05:12,310 --> 00:05:20,500 So when you are initializing and you one with this method set its name to whatever name I give you. 66 00:05:20,500 --> 00:05:29,020 If Let's let's call it him put forth a name that is input to a name. 67 00:05:29,020 --> 00:05:39,280 It means if there is an input for the name then also make our Fortran name to be input photo our name 68 00:05:40,360 --> 00:05:46,330 and or self what our name that's really irrelevant you could use it or not. 69 00:05:46,360 --> 00:05:52,830 I'm kind of I kind of prefer using the stuff and that's sort of it is input height. 70 00:05:53,500 --> 00:06:00,310 So when we call this method we introduce three values in it and name four to a name and a height and 71 00:06:00,310 --> 00:06:07,230 then our object gets produced based on those for what happens with the Y.D. we are producing it. 72 00:06:07,240 --> 00:06:09,280 But we don't have a new idea. 73 00:06:09,310 --> 00:06:13,900 So what they have to do is they have to manually add that to my in it as well which is I'm going to 74 00:06:13,900 --> 00:06:24,210 say I'm self-taught you ID will become you you ID Dar's you idea string perfect. 75 00:06:24,250 --> 00:06:27,560 So now when we initialize everything works the way we want it. 76 00:06:27,580 --> 00:06:30,120 Now how are we going to use this initialize method. 77 00:06:30,400 --> 00:06:33,750 I'm going to go here to I mean to use it but I'm making a stance. 78 00:06:33,930 --> 00:06:36,040 I'm going to save the user. 79 00:06:36,130 --> 00:06:43,150 And Knight says Oh use can only be initialized with this method because that in it has been overridden. 80 00:06:43,250 --> 00:06:44,160 And I said Oh. 81 00:06:44,230 --> 00:06:51,850 So the name is I mean your input photon on AM I'm going to say nil because it is an optional that can 82 00:06:51,850 --> 00:06:52,620 be nil. 83 00:06:52,780 --> 00:06:58,060 When I say I don't have a to a name I'm going to say the height is hundred eighty and that's about that. 84 00:06:58,060 --> 00:07:03,220 So I don't need any of these guys anymore because at the initialization everything has happened. 85 00:07:03,250 --> 00:07:09,450 So really quickly once again I'm going to change my format so you can see it a little bit easier. 86 00:07:09,570 --> 00:07:15,970 Maybe I bring this here which is a more common practice like that. 87 00:07:16,050 --> 00:07:20,730 And now if I bring that one down up there. 88 00:07:20,980 --> 00:07:21,310 OK. 89 00:07:21,310 --> 00:07:25,380 So now we have everything we have a user that has all these three. 90 00:07:25,540 --> 00:07:28,610 And then we have an in it for it that initializes it. 91 00:07:28,630 --> 00:07:34,180 And when I use the in it I'm going to provide it with a name no photo name and height. 92 00:07:34,240 --> 00:07:39,250 So far we wrote a class that has some attributes and has an initialized method. 93 00:07:39,250 --> 00:07:45,080 Now let's go ahead and write another class that this class and actually translate something for us. 94 00:07:45,130 --> 00:07:50,110 Of course we can write an entire translator but what I'm going to do is I'm gonna write a class that 95 00:07:50,110 --> 00:07:57,420 can translate to one senior word from English to either French or German languages. 96 00:07:57,460 --> 00:07:59,550 Let's see how we're going to do that. 97 00:07:59,600 --> 00:08:02,950 I'm going here I'm going to get myself into the space. 98 00:08:03,120 --> 00:08:07,830 Say I have a class called translate. 99 00:08:07,860 --> 00:08:08,260 All right. 100 00:08:08,260 --> 00:08:15,140 My translator class has a variable called language which is a string. 101 00:08:15,340 --> 00:08:27,640 So I have this one and it also has this it has an in its beat him language which is going to be a string. 102 00:08:28,130 --> 00:08:33,120 And whenever we do it obviously self-taught language becomes implied goit. 103 00:08:33,230 --> 00:08:40,480 So so far we have actually managed to initialize our translator and then I'm going to say this I'm going 104 00:08:40,480 --> 00:08:44,740 to say it has a function to translate. 105 00:08:44,780 --> 00:08:48,680 And what does translate do is the receivers of war. 106 00:08:48,740 --> 00:08:53,500 So let's say impute to war is going to be a string. 107 00:08:53,540 --> 00:08:55,960 It doesn't give us anything back. 108 00:08:56,240 --> 00:09:01,870 What it does do it has to translate that whole depending on what's the language. 109 00:09:01,880 --> 00:09:14,510 Some will say if language is let's say German then we assume that it that input word is always going 110 00:09:14,510 --> 00:09:15,560 to be hello. 111 00:09:15,800 --> 00:09:25,550 But you could you know write it differently on the say if language is German then prints hello. 112 00:09:25,650 --> 00:09:28,910 In your mind is it. 113 00:09:28,920 --> 00:09:30,390 And then hello. 114 00:09:30,930 --> 00:09:31,800 All right. 115 00:09:32,060 --> 00:09:35,340 Yes indeed. 116 00:09:35,510 --> 00:09:46,310 If that wasn't the case then when say hello in French is I was born or something like that. 117 00:09:46,700 --> 00:09:48,510 So let's say How are we going to use that. 118 00:09:48,530 --> 00:09:49,340 I'm going to say. 119 00:09:49,400 --> 00:09:50,840 Give me an object. 120 00:09:50,930 --> 00:10:03,960 So now my French is going to be translate initialize with a language called the French French and if 121 00:10:03,960 --> 00:10:10,990 now from my French I call a method called translate and I translate hello. 122 00:10:11,460 --> 00:10:20,730 Then if we run this it is going to show us hopefully hello in French is perfect and all the other things 123 00:10:20,730 --> 00:10:23,400 are printed because my other programs are still in here. 124 00:10:23,400 --> 00:10:24,530 My other class. 125 00:10:24,780 --> 00:10:27,300 So once again really really quickly. 126 00:10:27,390 --> 00:10:35,320 Classes read declare them class and then we have a set of attributes and we how we can have fun in it 127 00:10:35,330 --> 00:10:36,160 if we want. 128 00:10:36,210 --> 00:10:42,510 And we also can have functions if we need them and then depending on those we can do a variety of things 129 00:10:42,840 --> 00:10:46,830 something really quick about naming conventions. 130 00:10:46,830 --> 00:10:54,890 We often not often almost all of these we capitalize our class names so we don't color class translate 131 00:10:54,900 --> 00:10:59,960 we call it capital-T translate and that's a very very common practice. 132 00:11:00,030 --> 00:11:02,320 I would suggest that you know you do that as well. 133 00:11:02,340 --> 00:11:09,180 So in case you still need some help with this I'm going to keep this on the screen for a few seconds. 134 00:11:10,490 --> 00:11:15,410 And then after this I'm going to scroll to the other class there are first class and keep it here for 135 00:11:15,410 --> 00:11:16,950 a few seconds as well. 136 00:11:17,180 --> 00:11:22,690 So now that's these other two classes are the only thing that really matters is this. 137 00:11:22,690 --> 00:11:25,930 Class one attribute in it. 138 00:11:26,270 --> 00:11:29,340 And then this one a function. 139 00:11:29,360 --> 00:11:34,520 Beyond that everything is things that you have done in the past and it would be doing more and more 140 00:11:34,520 --> 00:11:36,010 of these in the coming Assen. 141 00:11:36,200 --> 00:11:37,280 So until then.